感觉学习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>
2. Javascript Prototype (1)
Javascript Prototype (2)
3. JavaScript 的几个 tip
分享到:
相关推荐
分析javascript中 prototype __proto__ constructor之间的关系
一直没弄清楚JavaScript中的prototype和constructor属性,今天看了看书,总算有点眉目了
this表示当前对象,如果在全局作用范围内使用this,则指代当前页面对象window,prototype本质上还是一个JavaScript对象,constructor始终指向创建当前对象的构造函数
在javascript的使用过程中,constructor 和prototype这两个概念是相当重要的,深入的理解这两个概念对理解js的一些核心概念非常的重要
本文实例讲述了JavaScript使用Prototype实现面向对象的方法。分享给大家供大家参考。具体分析如下: prototype 是 Function 对象的一个属性,这个属性指向另一个对象。 这个对象的所有属性和方法,都会被构造函数的...
对象的constructor属性用于返回创建该对象的函数,也就是我们常说的构造函数,除了创建对象,构造函数(constructor) 还做了另一件有用的事情—自动为创建的新对象设置了原型对象(prototype object)
真正的原因是:一个对象的constructor是它的构造函数的prototype.constructor,而每一个函数都有一个prototype,默认情况下,这个prototype有一个constructor属性,指向的是它自己。 我觉得Javascript的设计本意是让...
javascript中的this,constructor ,prototype,都是老生常谈的问题,深入理解他们的含义至关重要。在这里,我们再来复习一下吧,温故而知新! this this表示当前对象,如果在全局作用范围内使用this,则指代当前页面...
但是在Javascript语言体系中,是不存在类(Class)的概念的,javascript中不是基于‘类的’,而是通过构造函数(constructor)和原型链(prototype chains)实现的。但是在ES6中提供了更接近传统语言的写法,引入了...
这里有一点需要注意的是,每个函数都有一个prototype属性,这个prototype的constructor指向这个函数,这个时候我们修改这个函数的prototype时,就发生了意外。如 function Person(name,age){ this.name = name; ...
在javascript中,prototype、constructor以及__proto__之间有着“著名”的剪不断理还乱的三角关系,楼主就着自己对它们的浅显认识,来粗略地理理以备忘,有不对之处还望斧正。
b.constructor==BB.prototype.constructor) //true 这里的“有了”的执行过程是先查看b有没有此属性让后去查看prototype里的属性值,不是简单的A=B:如添加:b.constructor=”ccc”; 执行:alert(b.constructor=...
Javascript也是面向对象的语言,但它是一种基于...在prototype对象中又有一个constructor属性,这个constructor属性同样指向一个constructor对象,而这个constructor对象恰恰就是这个function函数本身。 是不是很绕?
JavaScript prototype(原型对象) 所有的 JavaScript 对象都会从一个 prototype(原型对象)中继承属性和方法。 在前面的章节中我们学会了如何使用对象的构造器(constructor): 实例 function Person(first, ...
这里有一点需要注意的是,每个函数都有一个prototype属性,这个prototype的constructor指向这个函数,这个时候我们修改这个函数的prototype时,就发生了意外。如 function Person(name,age){ this.name = name; this...
每个函数创建时默认带有一个prototype属性,其中包含一个constructor属性,和一个指向Object对象的隐藏属性__proto__。constructor属性的值为该函数的对象。在一个函数前面加上new来调用,则会创建一个隐藏连接到该...
Defines the properties and methods inherited by objects in the Attr Constructor prototype chain. BehaviorUrnsCollection Constructor Defines the properties and methods inherited by objects in the ...