《Constructors considered mildly confusing》
感觉学习javascript,还是要多看看外国人写的东西啊!!!.
还是有很多看不懂的地方。je上有人对此的探讨,现摘录一下。至于对错,以后再辨。
1.
In javascript, every object has a constructor property that refers to the constructor function that initializes the object.
这句话的大意是:
javascript中,每一个对象object 都有一个 constructor属性。而这个属性指向(引用)
初始化该对象的constructor 函数.
Constructor对创建对象的函数的引用(指针)。对于Object类,该指针指向原始的object()函数。
Prototype对该对象的对象原型的引用。对于所有的类,它默认返回Object对象的一个实例。
<script>
function Person(obj){
this.name = obj.name;
this.age = obj.age;
}
var one = new Person({
name : "helloWorld",
age : 22
});
alert(one.constructor == Person);
</script>
看到下面这段测试代码,我疯掉了。还是不知道怎么回事啊!!!
<script type="text/javascript">
function MyConstructor() {};
alert(MyConstructor.prototype); //[object Object]
alert(typeof MyConstructor.prototype); //object
alert(MyConstructor.prototype.constructor ); //function MyConstructor() {}
alert(typeof MyConstructor.prototype.constructor ); //function
alert(MyConstructor.prototype.constructor==MyConstructor); //true
alert(MyConstructor.prototype.constructor.prototype ); //[object Object]
alert(MyConstructor.constructor); // function Function() { [native code] }
alert(typeof MyConstructor.constructor); //function
alert(MyConstructor.constructor==Function); //true
alert(MyConstructor.constructor.prototype ); //function () {}
alert(typeof MyConstructor.constructor.prototype ); //function
alert(MyConstructor.constructor.prototype.constructor ); //function Function() { [native code] }
</script>
<script type="text/javascript">
function MyConstructor() {};
var myobject = new MyConstructor();
alert(myobject.prototype); // undefined
alert(typeof myobject.prototype); // undefined
alert(myobject.constructor); // function MyConstructor() {}
alert(typeof myobject.constructor); //function
alert(myobject.constructor==Function); //false
alert(myobject.constructor==MyConstructor); //true
alert(myobject.constructor.prototype ); // [object Object]
alert(typeof myobject.constructor.prototype ); //object
alert(myobject.constructor.prototype.constructor ); //function MyConstructor() {}
</script>
本文探讨了JavaScript中构造器(Constructor)的概念及其与对象之间的关系,通过具体示例展示了构造器、原型(Prototype)及它们之间的联系,并讨论了JavaScript原型继承的特点。
334

被折叠的 条评论
为什么被折叠?



