Javascript js构造函数

注解:1、构造函数没有new Object 过程,实则为后台自动创建;
2、This 指向 构造函数本身
3、构造函数无返回对象引用,实则为后台自动返回;但构造方法,需要有返回值时需要返回。
实例:
function Base(){ //自动创建为对象 ,不需要返回对象引用
this.elements =[];
this.getId = function(id){
this.elements.push(document.getElementById(id));
return this;
};
}

window.onload = function(){
var base = new Base(); //构造实例化

};

猜你喜欢

转载自blog.csdn.net/c15120067946/article/details/88220185