`

javascript constructor prototype

阅读更多

《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>
 

 

2.  Javascript Prototype (1)

    Javascript Prototype (2)

 

3.  JavaScript 的几个 tip

分享到:
评论

相关推荐

    prototype,__proto,constructor

    分析javascript中 prototype __proto__ constructor之间的关系

    JavaScript中的prototype和constructor简明总结

    一直没弄清楚JavaScript中的prototype和constructor属性,今天看了看书,总算有点眉目了

    JavaScript中几个重要的属性(this、constructor、prototype)介绍

    this表示当前对象,如果在全局作用范围内使用this,则指代当前页面对象window,prototype本质上还是一个JavaScript对象,constructor始终指向创建当前对象的构造函数

    深入分析js中的constructor和prototype

    在javascript的使用过程中,constructor 和prototype这两个概念是相当重要的,深入的理解这两个概念对理解js的一些核心概念非常的重要

    JavaScript使用Prototype实现面向对象的方法

    本文实例讲述了JavaScript使用Prototype实现面向对象的方法。分享给大家供大家参考。具体分析如下: prototype 是 Function 对象的一个属性,这个属性指向另一个对象。 这个对象的所有属性和方法,都会被构造函数的...

    JavaScript精炼之构造函数 Constructor及Constructor属性详解

    对象的constructor属性用于返回创建该对象的函数,也就是我们常说的构造函数,除了创建对象,构造函数(constructor) 还做了另一件有用的事情—自动为创建的新对象设置了原型对象(prototype object)

    Javascript的构造函数和constructor属性

    真正的原因是:一个对象的constructor是它的构造函数的prototype.constructor,而每一个函数都有一个prototype,默认情况下,这个prototype有一个constructor属性,指向的是它自己。 我觉得Javascript的设计本意是让...

    js老生常谈之this,constructor ,prototype全面解析

    javascript中的this,constructor ,prototype,都是老生常谈的问题,深入理解他们的含义至关重要。在这里,我们再来复习一下吧,温故而知新! this this表示当前对象,如果在全局作用范围内使用this,则指代当前页面...

    详解Javascript中prototype属性(推荐)

    但是在Javascript语言体系中,是不存在类(Class)的概念的,javascript中不是基于‘类的’,而是通过构造函数(constructor)和原型链(prototype chains)实现的。但是在ES6中提供了更接近传统语言的写法,引入了...

    浅谈javascript中的constructor

    这里有一点需要注意的是,每个函数都有一个prototype属性,这个prototype的constructor指向这个函数,这个时候我们修改这个函数的prototype时,就发生了意外。如 function Person(name,age){ this.name = name; ...

    图解prototype、proto和constructor的三角关系

    在javascript中,prototype、constructor以及__proto__之间有着“著名”的剪不断理还乱的三角关系,楼主就着自己对它们的浅显认识,来粗略地理理以备忘,有不对之处还望斧正。

    javascript new后的constructor属性

    b.constructor==BB.prototype.constructor&#41; //true 这里的“有了”的执行过程是先查看b有没有此属性让后去查看prototype里的属性值,不是简单的A=B:如添加:b.constructor=”ccc”; 执行:alert(b.constructor=...

    Javascript中的Prototype到底是什么

    Javascript也是面向对象的语言,但它是一种基于...在prototype对象中又有一个constructor属性,这个constructor属性同样指向一个constructor对象,而这个constructor对象恰恰就是这个function函数本身。 是不是很绕?

    JavaScript prototype(原型对象)

    JavaScript prototype(原型对象) 所有的 JavaScript 对象都会从一个 prototype(原型对象)中继承属性和方法。 在前面的章节中我们学会了如何使用对象的构造器(constructor): 实例 function Person(first, ...

    深入浅析JavaScript中的constructor

    这里有一点需要注意的是,每个函数都有一个prototype属性,这个prototype的constructor指向这个函数,这个时候我们修改这个函数的prototype时,就发生了意外。如 function Person(name,age){ this.name = name; this...

    JavaScript prototype属性深入介绍

    每个函数创建时默认带有一个prototype属性,其中包含一个constructor属性,和一个指向Object对象的隐藏属性__proto__。constructor属性的值为该函数的对象。在一个函数前面加上new来调用,则会创建一个隐藏连接到该...

    IE8 新增的Javascript 开发接口说明

    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 ...

Global site tag (gtag.js) - Google Analytics