Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Inheritance and Object Literal

In JavaScript, it is said that object literals have prototype linking, but function objects have both prototype linking and a prototype property.

So, based on the above, can one say that inheritance (which uses the prototype property), is possible only with function objects (constructor version) and not with object literals?

Also, to add, the __proto__ property is not accessible in all the browsers...

like image 947
copenndthagen Avatar asked Mar 07 '13 06:03

copenndthagen


People also ask

What is difference between object and object literal in JavaScript?

Objects created using object literal are singletons, this means when a change is made to the object, it affects the object entire the script. Whereas if an object is created using constructor function and a change is made to it, that change won't affect the object throughout the script.

What is an object literal JavaScript?

The Object literal notation is basically an array of key:value pairs, with a colon separating the keys and values, and a comma after every key:value pair, except for the last, just like a regular array. Values created with anonymous functions are methods of your object. Simple values are properties.

What is object inheritance in JavaScript?

When it comes to inheritance, JavaScript only has one construct: objects. Each object has a private property which holds a link to another object called its prototype. That prototype object has a prototype of its own, and so on until an object is reached with null as its prototype.

Can an object be literal?

Yes. In the object literal notation, an object description is a set of comma-separated name/value pairs inside curly braces. The names can be identifiers or strings followed by a colon. Save this answer.


1 Answers

Short version:

Yes: The prototype chain cannot be set up or modified by assigning to an object's prototype property. You cannot set up inheritance by creating an object with an object literal and then giving it a property called prototype. Such property will be called prototype, but will not be considered for prototypal inheritance.

Longer:

If you access a property that is undefined, that object's inheritance chain is checked. So, if obj['prop'] is undefined, then obj.prototype['prop'] will be checked. In many browsers, the prototype property is implemented internally as the __proto__ property, but that is besides the point. Rather, the point is that if some property is undefined, the object's prototype is checked for that property.

As people have said in comments, it's only possible to bestow an object with a prototype that provides the above-described inheritance by assigning that object to a function's prototype property and then using that function as a constructor.

But, the prototype property of an object produced by constructor invokation is not object.hasOwnProperty('prototype'). On the other hand, if you assign a prototype property to an object, then that object will object.hasOwnProperty('prototype'), but then object.prototype will have nothing to do with the prototype chain—it'll just be a regular property, and will be happened to call prototype.

To demonstrate this:

var foo = {};
foo.prototype = {bar: 'hello'};
console.log(foo.bar); // undefined
console.log(foo.prototype); // Object {bar: "hello"}
console.log(foo.hasOwnProperty('prototype')); // true

var Foo = function() {};
Foo.prototype = {bar: 'hello'};
var f = new Foo;
console.log(f.bar); // 'hello';
console.log(f.hasOwnProperty('bar')); // false
console.log(f.prototype); // undefined
console.log(f.hasOwnProperty('prototype')); // false
like image 122
Dmitry Minkovsky Avatar answered Sep 18 '22 07:09

Dmitry Minkovsky