[[Prototype]] vs prototype: ..what is the difference? (MyCons.__proto__ === MyCons.prototype) equals FALSE
Asked Answered
A

3

13

It seems like there is a difference here...

Let's say we have function MyConstructor() {}

MyConstructor's [[Prototype]] is Function.prototype, not MyConstructor.prototype.

In other (non-standard/"console.log-able") words:
MyConstructor.__ proto__ is not MyConstructor's MyConstructor.prototype

TRY THIS:

function MyConstructor() {};
(MyConstructor.__proto__ === MyConstructor.prototype); //false?! why?

Why is this so? Can someone explain it to me the difference?

Arbitress answered 26/2, 2012 at 9:15 Comment(1)
B
30

Think of it like this. MyConstructor is a function object, so it was created by Function; therefore its [[Prototype]] (or __proto__) is identical to Function.prototype.

In the same way, var myObj = new MyConstructor() creates an object myObj with a [[Prototype]] identical to MyConstructor.prototype.

To put it another way, functions have a prototype property, and when you invoke functions with new, they will construct an object having a [[Prototype]] identical to the constructor function's prototype property... however a function's prototype property is not the same thing as its [[Prototype]] (or __proto__) property, because a function follows the same rules as other objects and gets its internal [[Prototype]] property from the function that constructed it (which is always Function, incidentally).


To explain further, [[Prototype]] and prototype have entirely different purposes. [[Prototype]] is used when resolving an object's properties. If an object doesn't have a property, its [[Prototype]] is checked, and then that object's [[Prototype]], and so on, until either a property is found or you hit the end of the prototype chain.

In contrast, prototype is the mechanism by which you assign [[Prototype]] properties to objects, since you can't access them directly other than with the non-standard __proto__ property.

Since functions are objects, they have both a [[Prototype]] internal property, used to resolve properties as with normal objects, and a prototype property, which is assigned as the [[Prototype]] of new objects constructed by the function.

Barhorst answered 26/2, 2012 at 9:29 Comment(0)
I
2

this code will show it very clear:

var obj = {};
var arr = [];
var fun = function() {};    

A:     
console.log(obj.prototype);
console.log(arr.prototype);
console.log(fun.prototype);

B:
console.log(arr.__proto__);
console.log(fun.__proto__);

C:
console.log(obj.__proto__);
console.log(arr.__proto__.__proto__);
console.log(fun.__proto__.__proto__);
console.log(fun.prototype.__proto__);

the chrome console of that is:

enter image description here

the meaning of that:

A. in JavaScript 'prototype' is special object that exists just in functions - for making function constructor (for your question - that's why 'prototype' it's not equal to 'proto').

B. array and functions has the they're own 'proto' with all they're proprieties and methods that built in in JavaScript.

C. everything in JavaScript truly inside they're objects, so the 'prototype.proto' of functions, or 'proto.proto' of functions and arrays are all equal the 'proto' of an object.

Isaiasisak answered 28/9, 2017 at 7:25 Comment(3)
Wait, that code DOES work. As in, the "A: " and "B: " lines work. I just did "a:5" on in the console and it printed "5" but didn't create an "a" global variable. I'm confused. Normally you use these during object literals, but what is the use OUTSIDE of them? You can't use them as a label and 'goto' them or anything, so... Do you know?Inconsonant
So, I figured it out finally after finding the right way to search for it which was literally the phrase that matched this result: #34190756. So, it looks like Javascript has labels for CONTINUE statements that act as a goto ONLY. Honestly, that's one of the only valid uses of goto in my opinion. It's not really as bad as people make it out to be in modern languages like C# anymore since you can only do it in the same scope (thus preventing 99.9% of the speghetti code). It's just that there are ALWAYS better ways.Inconsonant
One last thing, it appears someone 'added' full 'goto' functionality into javascript. It works, you just have to do "[lbl] a:" instead of just a: and then run it through gotojs to have it transform it to the output. IF it's in a script tag on your site you can have all of them automaticall parsed (or using a specific 'type' like 'text/gotojs'). Interesting.Inconsonant
S
-1

Inspired by another answer, and to complete it:

var obj = {name: 'Ali'};
var arr = [1, 2, 3];
var date = new Date();
var func = function(){console.log('Hi!')};
var arrowFunc = ()=>{};

obj.prototype === undefined;
arr.prototype === undefined;
date.prototype === undefined;
func.prototype != undefined;
func.hasOwnProperty('prototype') === true;
arrowFunc.prototype === undefined;
arrowFunc.hasOwnProperty('prototype') === false;

obj.__proto__ === Object.prototype;
arr.__proto__ === Array.prototype;
date.__proto__ === Date.prototype;
func.__proto__ === Function.prototype;
arrowFunc.__proto__ === Function.prototype;

Array.prototype.__proto__ === Object.prototype;
Date.prototype.__proto__ === Object.prototype;
Function.prototype.__proto__ === Object.prototype;

Object.prototype__proto__ === undefined;

Still answered 1/6, 2020 at 8:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.