d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Javascript Code Doubt > Tiny Bit
Add Reply New Topic New Poll
Member
Posts: 37,598
Joined: Nov 19 2005
Gold: 3,405.00
Jun 15 2012 01:09pm
I don't get this piece of ECMAScript3 code, could some1 explain it?

Code
function inherit(p) {
   if (p == null) throw TypeError();
   var t = typeof p;
   if (t !== "object" && t !== "function")
       throw TypeError();
   function f() {};
   f.prototype = p;
   return new f();
}


It's about creating in a Object?
Member
Posts: 13,630
Joined: Dec 4 2009
Gold: 0.00
Jun 16 2012 12:04am
This is really easy to read. What's so hard to understand?

1. Function is defined with parameter p.
2. If p is null, then return an error.
3. Create a new variable called that is the data type for the variable p.
4. If t's value isn't "object" and "function", then return an error.
5. Define a new function f with no parameters and no code on the inside.
6. I don't really know what prototype does, but from the context I assume it makes f inherit p.
7. Return the function f.
Member
Posts: 37,598
Joined: Nov 19 2005
Gold: 3,405.00
Jun 16 2012 03:35am
Quote (BreakPoint @ Jun 16 2012 07:04am)
This is really easy to read. What's so hard to understand?

1. Function is defined with parameter p.
2. If p is null, then return an error.
3. Create a new variable called that is the data type for the variable p.
4. If t's value isn't "object" and "function", then return an error.
5. Define a new function f with no parameters and no code on the inside.
6. I don't really know what prototype does, but from the context I assume it makes f inherit p.
7. Return the function f.


thats it thanks
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll