druveen
druveen

Reputation: 1691

Need help for javascript oops

var a = function () {};
a.prototype.test  = function () {
alert("hello");
} 

works fine but in following code

var b = new Object();
b.prototype.test  = function () {
alert("hello");
} 

i am getting this error TypeError: Cannot set property 'test' of undefined and i am unable to get it.

As per my understanding b has inherited prototype object from Object. So we should be able to add a new property in following manner say b.prototype.x = 1 .

But Object.prototype.x = 1 works .

typeof Object and a gives function but that of b is object

I am not getting why b.prototype.x = 1 doesnt work

Thanks.

Upvotes: 4

Views: 151

Answers (4)

bchurchill
bchurchill

Reputation: 1420

This is a tricky issue -- there are two types of prototype fields, an internal one and an external one. The external one you can directly access with the normal prototype field as you are doing. The internal one is used to do lookups when a field/key is not found in the object.

If you do new blah(), it creates a new object whose internal prototype field is set to the external prototype field of blah. By default, the external prototype field of the newly constructed object is undefined. In particular, this is why evaluating b.prototype.x fails -- you can't do a field access on an undefined value. If you wish you can create a new object for a new external prototype, e.g. b = new Object(); b.prototype = {}.

You can see the internal/external prototype fields in action here:

Object.prototype.x = 4
b = new Object()
b.x // returns 4

What's happened is that b's internal prototype field points to Object.prototype, so lookups to b that fail are redirected to do a lookup in Object.prototype.

I'm not sure why, but newly created functions get their external prototype field set to Object -- this causes the first one to work.

Upvotes: 2

Kendall Frey
Kendall Frey

Reputation: 44374

Object is a function, which has a prototype property.

new Object() creates an object, which does not have a prototype property.


If you want to set the prototype of an object, you might mean setting the prototype of the object's constructor.

b.constructor.prototype.test = ...

To clarify some of the prototype/constructor nonsense:

A function has a prototype, which is an object. It specifies properties to add to an instance of that function.

An object has a constructor, which is a function. It specifies the function that was used to create the object.

Note that a function is an object, so it also has a constructor, which is Function.

Upvotes: 2

ryuusenshi
ryuusenshi

Reputation: 1986

You can refer to this resource

It doesn't work for the new object because it doesn't have a prototype property set. prototype property points to the Object that you have inherited. And since new Object() has had no object to inherit from, its prototype is set to undefined

A function on the other hand, has a super object (it inherited) by default.

Upvotes: 1

daryl
daryl

Reputation: 15207

You should be able to do it like this:

var b = {};
b.test = function() {
    alert('Hello');
};

Upvotes: 0

Related Questions