JavaScript 学习笔记 (3)

1.构造函数(对象)
  构造函数是用来初始化对象的。使用关键字new来调用构造函数创建对象。
调用构造函数的一个重要特征是构造函数的prototype属性用作新对象的原型。这意味着同一个
构造函数调用的对象继承自同一个原型。这意味着它们都是同一个类的成员。

2.类名
  构造函数的名字通常用作类名。

function test( a,b)
{
	//这2个属性是不可继承的,每个对象都拥有唯一的属性。
	this.name = a;
	this.age = b;
}
test.prototype = {
	toString:function(){ 
		return "("+this.name+","+this.age+")";
	}
}
var a = new test('a',1);
var b = new test('b',2);
console.log(a.toString()); // '(a,1)'
console.log(b.toString());// "(b,2)"

3.prototype 和 constructor
  每一个函数都有一个prototype 属性 ,它的值是一个对象,包含一个不可枚举的属性constructor 。constructor 的值是一个函数对象,指向构造函数。(注意:在上面的例子中,我们重写了test.prototype对象,因此是没有constructor属性的)

function a()
{
}
var b = new a();
b.constructor == a;    // true 
a.prototype.constructor == a;.//true

b.constructor 是继承过来的 . 普通对象是没有 prototype 属性的

4.instanceof
检测某个对象是否是属于某个类。实际上检测的是该对象是否继承自这个类的prototype

function a()
{
	
}
var b = new a();
b instanceof a ;  // true
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值