function Stu(name,age){
this.name=name;
this.age=age;
}
// 不希望打印出原型的属性,只打印自身属性
for(let item of f){ if(f.hasOwnProperty(item)){ console.log(item) } }
let f =new Foo();
f.alertName= function(){
// 方法具体实现
}
f.toString() //toString方法在Foo原型中找,找不到接着要去 f.__proto__.__proto__中查找
function Elem(id){
this.elem =document.getElementById(id);
}
Elem.prototype.html =function(val){
if(val){
return this.elem.innerHTML;
}
this.elem.innerHTML= val;
return this; //将Elem对象返回
}
Elem.prototype.on =function(type,func){
this.elem.addEventListener(type,func);
return this;
}
const elem =new Elem(‘div1‘);
elem.html(‘张三‘).on(‘click‘,function(){
console.log(‘点击了文本,打印张三‘);
}).html(‘李四’)
原文:https://www.cnblogs.com/fuGuy/p/9206784.html