473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Setting __proto__

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
8 2461
Dormilich
8,658 Expert Mod 8TB
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
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
8,658 Expert Mod 8TB
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
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
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
8,658 Expert Mod 8TB
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
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
8,658 Expert Mod 8TB
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

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

Similar topics

6
by: Niyazi | last post by:
Hi, We have and IBM AS400 and I belive the reional setting is Turkish. The IBM Client-Access for Windows that install in our PC (WIN XP SP2) set to Turkish characters. Now my PC has English...
18
by: Dixie | last post by:
Can I set the Format property in a date/time field in code? Can I set the Input Mask in a date/time field in code? Can I set the Format of a Yes/No field to Checkbox in code? I am working on...
0
by: Shravan | last post by:
Hi, I have a Windows Forms Custom DataGrid, which is put in a usercontrol, which on setting DataSource is setting focus to grid. The call stack for setting the focus is as follows. This is not...
2
by: junlia | last post by:
Hi All, I am working on a project that acts as a bridge. It does some checking with post xml data, and then redirects the request to an appropriate page. However, we find that depends on the...
1
by: Dachshund Digital | last post by:
I know there must be a way to do this... in .NET 2.0? I have a situation where I know the name of the setting, and want to pass it into a function, and set or get the corresponding setting. ...
6
by: mmcloughlin | last post by:
I'm learning about objects and am trying to figure out how basic inheritance works. I've got into the habit of explicitly setting the prototype object with an object literal as it seems to make the...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
6
by: cbare | last post by:
Hello JS Gurus, One thing I haven't figured out about javascript is the treatment of __proto__. Inheritence, whether prototypes or class-based, is just a shorthand form of delegation (leaving...
6
by: metaperl | last post by:
I would like to check the setting of this variable in our MS-SQL 2000 database. Also, is there info on what the default value of this variable is?
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.