日期:2014-05-16  浏览次数:20430 次

JAVASCRIPT 中对象冒充应用
function father (sex,name)
{

  this.name=name;
this.sex=sex;


this.hao=function ()
{
alert(this.sex+","+this.name );
}
  }
  function child (sex,name,age)
{
this.method=father; //使FATHER成为自己的方法
// 使NAME成为自己的属性

this.method(name);
this.method(sex);
delete this.method;
  this.age=age;
this.good=function () {
alert(this.age);
}
  }
var fa=new father("man","he");
var ch=new child("woman","wei","26");

fa.hao();
ch.hao();
ch.good();


  当同时传两中参数时有一个UNDEFINED,为什么呢?

------解决方案--------------------
this.method(name);
this.method(sex);
 
这两句合为一句,不然前一次的赋值就被覆盖,改为:
this.method(sex,name);

------解决方案--------------------
JScript code

function father (sex,name){
    this.name=name;
    this.sex=sex;
    this.hao=function (){
        alert(this.sex+","+this.name );
    }
}
function child (sex,name,age){
    this.method=father; //使FATHER成为自己的方法
    // 使NAME成为自己的属性
    //this.method(name);
    //this.method(sex);
    this.method(sex,name);
    delete this.method;
    this.age=age;
    this.good=function () {
        alert(this.age);
    }
}
var fa=new father("man","he");
var ch=new child("woman","wei","26");

fa.hao();
ch.hao();
ch.good();

------解决方案--------------------
伪继承是吧

this.method=father;
// 下面两行错了 你这样写 只相当于给father传递了一个参数sex 
this.method(name);
this.method(sex);

看到没你的函数: function father (sex,name)
this.method(sex , name)

建议你用apply或者call方法