javascript constructor prototype

本文探讨了JavaScript中构造器(Constructor)的概念及其与对象之间的关系,通过具体示例展示了构造器、原型(Prototype)及它们之间的联系,并讨论了JavaScript原型继承的特点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

《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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值