构造函数的首字母大写,行业约定,它是构造函数,要用new来调用。
var Person = function(name,pwd){
//对this的理解,分下面三步=====>下面用new来调用这个函数,所有1和3省略,保留2就可以了
//1.创建一个空对象,用this来引用
//var this = {};
//2.将用户自定义的属性和方法(name,pwd,getInfo)添加到这个用this引用的新对象上面
this.name = name;
this.pwd = pwd;
this.getInfo = function(){
return this.name + "的密码是:" + this.pwd}
//3.返回这个新对象this
//return this;
}
//构造函数是用来生成对象
var person1 = new Person('admin','123562');
网友评论