After experimenting so much time, i figured out that __proto__
or Object.getPrototypeOf()
method is the correct way to traverse prototype chain in DOM objects.
Using a series of constructor.prototype actually doesn't traverse the prototype chain in both browsers.(while this is the way defined in ECMA standard, prototype property of constructor is your prototype object).
Any suggestion or comment is welcomed...
p1 = document.getElementById("test"); // div element
//Prototype Object of p1
p2 = element.constructor.prototype;
//Prototype object of p2
p3 = element.constructor.prototype.constructor.prototype;
console.log(p2 === p3); // true in chrome(howcome they same ?), false in firefox
q2 = element.__proto__;
q3 = element.__proto__.__proto__;
console.log(q2 === q3); // false in both browser
Short answer. So what's the difference between constructor and prototype? A short answer is that the constructor is a function that is used to create an object, while the prototype is an object that contains properties and methods that are inherited by objects created from a constructor.
prototype is a property of a Function object. It is the prototype of objects constructed by that function. __proto__ is an internal property of an object, pointing to its prototype. Current standards provide an equivalent Object.
prototype. constructor. The constructor property returns a reference to the Object constructor function that created the instance object. Note that the value of this property is a reference to the function itself, not a string containing the function's name.
The __proto__ getter function exposes the value of the internal [[Prototype]] of an object. For objects created using an object literal, this value is Object. prototype . For objects created using array literals, this value is Array. prototype .
I totally agree with Boris... You should search here for more details (https://www.google.com/search?q=javascript+prototype+chain) but basically if you want to browse elements in the DOM object, you just need to do this like hereunder :
function exploreElement(element){
contentToString = "";
for (var i in element){
contentToString += i + " : " + element[i] + "<br />";
}
document.write(contentToString);
}
exploreElement(document);
The prototype and proto are something totally different...
If you have a constructor function like this :
function SomeObject(){
this.__proto__.id = "instance_default_name";
SomeObject.id = "SomeObject";
// __proto__ HERE to access the prototype!!!
}
You can then add methods to this Constructor via prototype (I assume that you have an empty div with an id "myInstance" and another with the id "test" in the document):
SomeObject.prototype.log = function(something){
document.getElementById("myInstance").innerHTML += something + "<br />";
}
Add some methods for test purposes :
SomeObject.prototype.setId = function(id){
this.id = id;
}
SomeObject.prototype.getId = function(){
return this.id;
}
SomeObject.prototype.getClassName = function(){
return SomeObject.id;
}
Then, you may instantiate SomeObject with the new operator and do some tests like this :
myInstance = new SomeObject();
myInstance.setId("instance_1");
aDivElement = document.getElementById("test");
aDivElement.style.backgroundColor = "rgb(180,150,120)";
myInstance.log("p1 = " + aDivElement);
// [object HTMLDivElement]
myInstance.log("p1 backgroundColor = " + (aDivElement.style.backgroundColor));
myInstance.log("myInstance = " + myInstance);
// [object Object] an instance of SomeObject
myInstance.log("myInstance.constructor = " + myInstance.constructor);
// function SomeObject() { this.__proto__.id = "instance_default_name"; SomeObject.id = "SomeObject"; }
myInstance.log("myInstance.constructor.prototype = " + myInstance.constructor.prototype);
// .prototype WHEN CALLED by the instance NOT __proto__
// The constructor of myInstance is SomeObject and the prototype of SomeObject is the prototype of all instances of SomeObject
myInstance.log("myInstance.id = " + myInstance.getId());
// id for the instance of SomeObject that you have instanciated
myInstance.log("SomeObject.prototype.id = " + SomeObject.prototype.getId());
// id by default of the prototype
myInstance.log("myInstance.constructor.prototype.id = " + myInstance.constructor.prototype.getId());
// id by default of the prototype
myInstance.log("myInstance.getClassName() = " + myInstance.getClassName());
// myInstance.getClassName() = SomeObject
I don't know if this talks to you a little, but I hope this will help you in your search. Best regards. Nicolas
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With