0

--
Hello SO, Hoping for some minor guidance on the issue of .prototype.

I've gone through all the answers in SO and they did not seem to cover this specific question, or maybe they did but I did not understood it like that.

The question at hand (and code)

function foo(){};
foo.prototype.type = 'foo';
function bar()
{
    this.prototype = foo.prototype;
}
test = new bar();
console.log(test.type); // type is undefined

The question
From what I understand, the request for the type had to cascade up the prototype chain until it found the foo prototype, This did not happen, Obviously I'm understanding something wrong - Why is type undefined?

I'm basically trying to find a way to extend a function object so that

new foo() - return a foo object
new bar() - return a bar object that has all the methods and properties of foo.

I appreciate any help or reference I can get!

Patrick
  • 3,289
  • 2
  • 18
  • 31
  • Please understand [the difference between the prototype chain and the `.prototype` property](http://stackoverflow.com/questions/9959727/proto-vs-prototype-in-javascript), then check out [Correct javascript inheritance](http://stackoverflow.com/q/10898786/1048572) – Bergi Jun 04 '14 at 20:55
  • Following what lante said, I think I got the concept. .prototype property is really just an object with some attributes. If an attribute is missing from the current object, It goes up the chain and so forth. Does that sound about right? – Patrick Jun 04 '14 at 21:18
  • Yes. Emphazising that "the chain" is *not* the `.prototype` property of the object on which the attribute is looked up. – Bergi Jun 04 '14 at 21:42

2 Answers2

1

Well, when you do this:

function bar()
{
    this.prototype = foo.prototype;
}

you are not changing the bar object prototype but assigning a new property to the bar object called prototype, which has the foo object prototype, basically: { type: 'foo' }.

then:

test = new bar();
console.log(test.type); // type is undefined

of course is undefined! you never define it, you just define the prototype property

console.log(test.prototype); // I have surprises for you

I think you want somethink like inheritance. I suggest the Crockford way of inheritance:

Function.prototype.inherits = function (Parent) {
    this.prototype = new Parent();
    return this;
};

Then, just do:

bar.inherits(foo);

Now,

test = new bar();
console.log(test.type); // foo!

Hope this helps

lante
  • 7,192
  • 4
  • 37
  • 57
  • 1
    That's not the Crockford way of inheritance, that's just wrong. – Bergi Jun 04 '14 at 20:53
  • Hm, I stand corrected and take back the first part of my claim. I thought Crockford [would have learned](http://javascript.crockford.com/prototypal.html) (which still is not [perfect](http://stackoverflow.com/a/14267886/1048572)). – Bergi Jun 05 '14 at 17:43
0

Thanks for the comments and replies, to extend lante's answer to what I think is fully explaining the situation.

A JavaScript function is of course an object, When you instantiate a new instance of a function object, The created object receives the prototype of that function.

for instance,

var Foo = fn() {
    this.ok= 'this is okay';
}

var Bar = fn(){};
Bar.prototype = new Foo(); // Bar.prototype is now a regular object, nothing fancy.

//for all intents and purposes, this is exactly like doing this :  
Bar.prototype = {ok : 'this is okay'}; //just assigns an object, reduntent

var test = new Bar(); //new Bar instance

console.log(test.ok); //prints 'bar'

What's the magic?
test has no ok property, But when it's being called it chains up to the function object's prototype and tries to find it there, if it can't, it keeps moving up until it reaches the end.

Thanks again for all the answers

Patrick
  • 3,289
  • 2
  • 18
  • 31