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.sex}`);
};
// 静态属性与静态方法
Person.setName = function() {
console.log("静态方法");
console.log(this.name); //undefined
};
Person.weight = 180;
const p = new Person("JonSnow", 21);
// 实例方法通过实例化调用
p.run();
p.work();
// 静态方法通过类名直接调用
Person.setName();
console.log(Person.weight);
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.sex}`);
};
const p = new Person("JonSnow", 21);
const p1 = new Person("Cercei", 22);
console.log(p.sex); //男
console.log(p1.sex); //男
网友评论