普通函数与构造函数的区别
function a(name){
console.log(name);
}
a('zb') // 普通函数
function Person(name,age){
this.name = name
this.age = age
this.getName = function(){
return this.name
}
}
let zb = new Person('zb','88') // 构造函数,使用new关键字
1,用new关键字来进行调用的函数称为构造函数。2,一般首字母大写。
构造函数成员
构造函数成员分为静态成员和实例成员
function Person(name,age){
this.name = name //实例成员
this.age = age //实例成员
this.getName =function(){ //实例成员
console.log(this);
}
}
Person.sex = '男' // 静态成员
Person.getSex = function(){ //静态成员
console.log(this.sex)
}
let zb = new Person('zb','88')
静态成员只能由构造函数调用,实例对象只能由实例化对象调用
let zb = new Person('zb','88')
console.log(zb.name); //调用实例成员 zb
zb.getName() //调用实例成员 zb
Person.getSex() //调用静态成员 男
console.log(Person.sex); //调用静态成员 男
静态成员函数中this指向构造函数,实例对象函数中this指向实例化对象