您的位置:首页 > 其它

在Function对象上扩展method方法

2016-05-26 17:12 411 查看
;(function() {
/**
* 在Function对象上扩展method方法
* @param  {String}   name     扩展的方法名称
* @param  {Function} callback 方法的定义
* @return {Function} return this  提供链式调用
*/
if(typeof Function.prototype.method !== 'function'){
Function.prototype.method = function(name, callback) {
// 在当前function上扩展name方法
this.prototype[name] = callback;
// 便于method方法的链式调用
return this
}
}

// method方法的运用
var Hoogle = function() {
this.name = "Hoogle",
this.major = "F2E"
};

// 扩展hoogle构造函数上的原型方法
Hoogle.method("getName", function() {
return this.name
}).method("setName", function(newname) {
this.name = newname;
return this
}).method("getMajor", function() {
return this.major
}).method("setMajor", function(newmajor) {
this.major = newmajor;
return this
});

// 实例化Hoogle
var hoogle = new Hoogle();
// 设置静态属性并输出结果
hoogle.setName("newHoogle").setMajor("newMajor");
console.info(hoogle.getName(), hoogle.getMajor());

})();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: