以下是对构造函数的浅薄理解:
构造函数一般以大写字母开头命名; 如:
fuction Preson(){...}
调用 :
任何函数,只要通过 new 操作符来调用,那它就可以作为构造函数 ;
deom:
function Preson(name,age,sex){
this.name = name;
this.age = age;
this.sex = sex;
this.sayName = function(){
console.log(this.name);
}
this.sayAge = function(){
console.log(this.age);
}
this.saySex = function(){
console.log(this.sex);
}
}
//普通函数调用
Preson("飞飞","18","男");//this 指向 window
window.sayName();
window.sayAge();
window.saySex();
//构造函数调用
var preson = new Preson("飞哥","19","男"); //this 指向 preson
preson.sayName();
preson.sayAge();
preson.saySex();
欢迎同行讨论