Tôi hoàn toàn đồng ý với Boris ... Bạn nên tìm kiếm ở đây để biết thêm chi tiết (https://www.google.com/search?q=javascript+prototype+chain) nhưng về cơ bản nếu bạn muốn duyệt các yếu tố trong các đối tượng DOM, bạn chỉ cần làm dưới đây như thế này:
function exploreElement(element){
contentToString = "";
for (var i in element){
contentToString += i + " : " + element[i] + "<br />";
}
document.write(contentToString);
}
exploreElement(document);
Nguyên mẫu và proto là một cái gì đó hoàn toàn khác nhau ...
Nếu bạn có một hàm constructor như thế này:
function SomeObject(){
this.__proto__.id = "instance_default_name";
SomeObject.id = "SomeObject";
// __proto__ HERE to access the prototype!!!
}
Sau đó bạn có thể thêm các phương pháp để Constructor này qua nguyên mẫu (tôi giả sử rằng bạn có một div trống với một id "myInstance" và khác với id "test" trong tài liệu):
SomeObject.prototype.log = function(something){
document.getElementById("myInstance").innerHTML += something + "<br />";
}
Thêm một số phương pháp cho mục đích thử nghiệm:
SomeObject.prototype.setId = function(id){
this.id = id;
}
SomeObject.prototype.getId = function(){
return this.id;
}
SomeObject.prototype.getClassName = function(){
return SomeObject.id;
}
Sau đó, bạn có thể nhanh chóng SomeObject với các nhà điều hành mới và làm một số xét nghiệm như thế này:
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
Tôi không biết điều này có nói chuyện với bạn một chút hay không, nhưng tôi hy vọng điều này sẽ giúp bạn tìm kiếm. Trân trọng. Nicolas