构造函数
function Person(){
name:'zhangsan';
age:18;
run:function(){
console.log("run")
}
}
let a = new Person()
a.run()
静态属性 / 静态方法
function Person(){
this.name:'zhangsan';
this.age:18;
this.run:function(){ // 实例方法
console.log("run")
}
}
Person.name = '哈哈哈' // 静态属性
Person.getInfo = function(){ // 静态方法
console.log('静态方法')
}
Person.name
Person.getInfo() // 静态方法直接类调用即可
let a = new Person() // 实例方法调用需要new 实例对象
a.run()
说明:静态方法和属性,比如jquery中的 , , ,.get,就是直接调用$类上的静态方法可以直接拿过来使用
原型链
function Person(){
this.name:'zhangsan';
this.age:18;
this.run:function(){
console.log("run")
}
}
Person.prototype.sex = '男' // 原型链
Person.getInfo = function(){
console.log('静态方法')
}
let a = new Person()
a.run()
继承
// 方法1:对象冒充实现继承:可以继承构造函数里面的方法和属性,凡是不继承原型链
function Web(){
Person.call(this)
}
let w =new Web();
w.run()
// 方法2:原型链继承:既可以继承构造函数里面的方法和属性,也可以继承原型链
function Web(){
...
}
Web.prototype = new Person()
let w =new Web();
// let w = new Web('张三',20);
w.sex
弊端:原型链继承时给父类传参时在调用方法时,方法获取不到传参过去的属性,打印出的是undefined
// 方法3:对象冒充+原型链继承的方法实现
// 解决原型链继承弊端
function Web(){
Person.call(this,name,age)
}
Web.prototype = new Person()
// 或 Web.prototype = Person.prototype;
let w =new Web('张三',20);
w.sex
w.run()