相关文章
prototype constructor __prpto__ 三者之间关系
好玩的:
1.字符串函数扩展 ( 一般调用 )
String.prototype.repeat=function(num){
return (new Array(num+1)).join(this)
}
console.log('good'.repeat(3))
2. 自定义原型名称 ( 配置调用 )
Function.prototype.method = function (name, func) {
if (!this.prototype[name]) {
this.prototype[name] = func;
};
return this;
}
// 取整
Number.method("absolute", function () {
return Math[this < 0 ? "ceil" : "floor"](this);
});
console.log((-3.53).absolute());
// 移除字符串末端空白
String.method("trim", function () {
return this.replace(/^\s+|\s+$/g, '');
})
console.log(" meat ".trim());