JavaScript对象表示方式

本文介绍了JavaScript中创建对象的多种方式,包括使用字面量、构造函数、原型链及支持链式调用的方法,并对比了不同方式的特点。

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

/**
* Created by zengxianwen on 2016/6/13.
*/
/**
* 对象之字面量表示方式
@type {{checkName: checkObject.checkName, checkEmail: checkObject.checkEmail, checkPwd: checkObject.checkPwd}}
*/
var checkObject = {
checkName:function () {
console.log('checkName');
},
checkEmail:function () {
console.log('checkEmail');
},
checkPwd:function () {
console.log('checkPwd');
}
}
checkObject.checkName();
/**
* 对象之另一种表示方式
*/
var checkObject_1 function () {}

checkObject_1.checkName function () {
console.log('checkObject_1.checkName')
}
checkObject_1.checkEmail function () {
console.log('checkObject_1.checkEmail')
}
checkObject_1.checkPwd =function(){
console.log('checkObject_1.checkPwd')
}

checkObject_1.checkName();

/**
* 类之本身表示方式
@constructor
*/
var CheckObject function () {
this.checkEmail function () {
console.log('CheckObject.checkEmail');
return
};
this.checkName function () {
console.log('CheckObject.checkName');
};
this.checkPwd function () {
console.log('CheckObject.checkPwd');
};
}
var new CheckObject();
c.checkName();

/**
* 减小开销,类之原型表示方式
@constructor
*/
var CheckObject_1 function () {};
CheckObject_1.prototype = {
checkName:function () {
console.log('CheckObject_1.checkName');
},
checkEmail:function () {
console.log('CheckObject_1.checkEmail');
},
checkPwd:function () {
console.log('CheckObject_1.checkPwd');
}
}
new CheckObject_1().checkEmail();
console.log('以下写法为,访问方式为链式访问方式');
/**
* 类之本身表示方式,访问方式为链式
@constructor
*/
var CheckObject_2 function () {
this.checkEmail function () {
console.log('CheckObject_2.checkEmail');
return this;
};
this.checkName function () {
console.log('CheckObject_2.checkName');
return this;
};
this.checkPwd function () {
console.log('CheckObject_2.checkPwd');
return this;
};
}

var c1 new CheckObject_2();
c1.checkName().checkEmail();

/**
* 减小开销,类之原型表示方式,访问方式为链式访问方式
@constructor
*/
var CheckObject_3 function () {};
CheckObject_3.prototype = {
checkName:function () {
console.log('CheckObject_3.checkName');
return this;
},
checkEmail:function () {
console.log('CheckObject_3.checkEmail');
return this;
},
checkPwd:function () {
console.log('CheckObject_3.checkPwd');
return this;
}
}
new CheckObject_3().checkEmail().checkName();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值