By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,299 Members | 1,054 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,299 IT Pros & Developers. It's quick & easy.

Setting __proto__

P: 62
I am confused as to why __proto__ is now deprecated and something less flexible - getPrototypeOf - is recommended. Having a __proto__ property will allow you to set the object "type"/methods of a currently existing object, which cannot be achieved with Object.getPrototypeOf and Object.create. One application that I can think of is if you have a drawing tool, and you want to allow users to switch between shapes with similar parameters but display differently, e.g. rectangle, rounded rectangle, parallelogram, etc. (like Microsoft Office Word). Then it would be good to have a display function for each "type" of shape, and you switch the shape by changing the __proto__ property. Would there be any way to do this?
Mar 3 '12 #1
Share this Question
Share on Google+
8 Replies


Dormilich
Expert Mod 5K+
P: 8,639
I am confused as to why __proto__ is now deprecated
because itís non-standard and JavaScript is complicated enough with its browser-specific extensions.

besides that Object.getPrototypeOf() serves a totally different purpose (direct access to the prototype chain, which is not possible otherwise from the object instances)

One application that I can think of is if you have a drawing tool, and you want to allow users to switch between shapes with similar parameters but display differently, e.g. rectangle, rounded rectangle, parallelogram, etc. (like Microsoft Office Word). Then it would be good to have a display function for each "type" of shape, and you switch the shape by changing the __proto__ property. Would there be any way to do this?
make a method for each shape. you may use other methods inside that method to accomplish common tasks. you can also use a Decorator Pattern (or use inheritance).

besides that you should never touch the inheritance chain to influence an instance methodís behaviour (you can, of course, define an object property for type, which you use in the drawing method)
Mar 3 '12 #2

P: 62
Isn't Object.getPrototypeOf(obj) just going to be a reference to the obj.__proto__ object?

I thought making methods for each shape would defeat the purpose of object-oriented programming? I suppose what you mean is to have a super-shape that has a shape type, and the display method would have to "switch case" for each type, and that is not really good when you could have done it with different classes that inherit a parent class (shape) having some base methods such as line/fill style. But if the user wants to "Change Autoshape", without the __proto__ switching you would have to create a new object and copy everything to it.
Mar 5 '12 #3

Dormilich
Expert Mod 5K+
P: 8,639
Isn't Object.getPrototypeOf(obj) just going to be a reference to the obj.__proto__ object?
nope. getPrototypeOf() accesses the prototype property.

MDC about __proto__
Description

When an object is created, its __proto__ property is set to constructing function's prototype property. For example var fred = new Employee(); will cause fred.__proto__ = Employee.prototype;.

This is used at runtime to look up properties which are not declared in the object directly. E.g. when fred.doSomething() is executed and fred does not contain a doSomething, fred.__proto__ is checked, which points to Employee.prototype, which contains a doSomething, i.e. fred.__proto__.doSomething() is invoked.

Note that __proto__ is a property of the instances, whereas prototype is a property of their constructor functions.
Mar 5 '12 #4

P: 62
Sorry, but I think you are really wrong about this. prototype is a property of constructor functions, while Object.getPrototypeOf(obj) will get the prototype link of obj.
Mar 6 '12 #5

P: 62
To make it clear, x.prototype is not equal to Object.getPrototypeOf(x), even if x is a function. Object.getPrototypeOf(function Square(length){ this.length=length; }) will return Function.prototype and not Square.prototype.

Try this:
Expand|Select|Wrap|Line Numbers
  1. function Square(length){ this.length=length; }
  2. alert(Object.getPrototypeOf(Square)==Function.prototype);
  3. alert(Object.getPrototypeOf(Square)==Square.prototype);
Mar 6 '12 #6

Dormilich
Expert Mod 5K+
P: 8,639
Sorry, but I think you are really wrong about this. prototype is a property of constructor functions, while Object.getPrototypeOf(obj) will get the prototype link of obj.
let me correct myself: getPrototypeOf() accesses the internal [[Prototype]] property. (cf. ECMAScript 262 5.1 sections 15.2.3.2 & 8.6.2)
Mar 6 '12 #7

P: 62
So what I was saying was that __proto__ is really what's retrieved by Object.getPrototypeOf() but there is no Object.setPrototypeOf(). And I heard the original "recommended" way to have inheritance was using:
Expand|Select|Wrap|Line Numbers
  1. NewClass.prototype.__proto__=SuperClass.prototype;
So now I suppose it is:
Expand|Select|Wrap|Line Numbers
  1. NewClass.prototype=Object.create(SuperClass.prototype);
Mar 6 '12 #8

Dormilich
Expert Mod 5K+
P: 8,639
So what I was saying was that __proto__ is really what's retrieved by Object.getPrototypeOf() but there is no Object.setPrototypeOf(). And I heard the original "recommended" way to have inheritance was using:
Expand|Select|Wrap|Line Numbers
  1. NewClass.prototype.__proto__=SuperClass.prototype;
actually, I’ve never seen extend code to use __proto__ before.
Mar 6 '12 #9

Post your reply

Sign in to post your reply or Sign up for a free account.