原型链和构造函数的区别

function Person(name,age){
    //构造函数里面的方法和属性
    this.name=name;
    this.age=age;
    this.run=function(){
        console.log(`${this.name}------${this.age}`);
    }
}
//原型链上面的属性和方法   原型链上的属性和方法可以在所有实例中共享,构造函数中的属性和方法不能在实例中共享
Person.prototype.sex='男'
Person.prototype.work=function(){
    console.log(`${this.name}---${this.age}---${this.sex}`)
}
//静态方法
Person.setName=function(){
    console.log('静态方法');
}
var p=new Person('zhangsan','20');
p.run();
p.work();
Person.setName();//执行静态方法,实例上无静态方法
原文地址:https://www.cnblogs.com/em2464/p/11377998.html