当前位置: 代码迷 >> JavaScript >> js 获取类属性有关问题
  详细解决方案

js 获取类属性有关问题

热度:233   发布时间:2013-03-22 09:49:50.0
js 获取类属性问题

//定义父类
function InputText() {
    this.id = '';
    this.name = '';
    this.type = {'text':'text','password':'password','hidden':'hidden'};
}
//表单元素的前缀名称
InputText.prototype.prefix = 'InputText_';


//定义子类
 function InputRadio() {
     this.checked = {'false':'false','true':'true'};
 }
 InputRadio.prototype = new InputText();
 InputRadio.prototype.constructor = InputRadio;
 InputRadio.prototype.foo = function () { alert(this.prefix);};

//如何获取子类与父类构造函数里面的属性呢?
结果应该是:id,name,type,checked

求达人解决方案....

------解决方案--------------------

function InputText() {
    this.id = '';
    this.name = '';
    this.type = {'text':'text','password':'password','hidden':'hidden'};
}
//表单元素的前缀名称
InputText.prototype.prefix = 'InputText_';
 
 
//定义子类
 function InputRadio() {
     this.checked = {'false':'false','true':'true'};
     InputText.call(this);
 }
 InputRadio.prototype = new InputText();
 InputRadio.prototype.constructor = InputRadio;
 InputRadio.prototype.foo = function () { alert(this.prefix);};

var a = new InputRadio();
for(i in a){
 a.hasOwnProperty(i) && console.log(i);
}
  相关解决方案