这个一共弹出3个对话框:blue,green,green;
但objB.sayColor();输出的应该是red才对啊.
如果要是ClassA的属性name与那个ClassB的name重复的话.但我已经在这个this.name = sName;前面删除了ClassA的方法了啊?
大家能说说原因吗
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=bg2312" />
<meta name="author" content="Chomo" />
<link rel="start" href="http://www.14px.com" title="Home" />
<title>利用box-sizing实现div仿框架</title>
<style type="text/css">
* { margin:0; padding:0;}
</style>
<script type="text/javascript">
function ClassA(sColor){
this.name = sColor;
this.sayColor = function(){
alert(this.name);
}
}
function ClassB(sColor,sName){
this.newMothod = ClassA;
this.newMothod(sColor);
delete this.newMothod;
this.name = sName;
this.showName = function(){
alert(this.name);
}
}
var objA = new ClassA("blue");
var objB = new ClassB("red","green");
objA.sayColor();
objB.sayColor();
objB.showName();</script>  
</head>
<body>  
</body>
</html>

解决方案 »

  1.   

    this.name=sColor;//(ClassA)this.name=sName;//(ClassB)alert(this.name);你直接运行上面看看 你的ClassB初始化的逻辑总结下来 就是这个意思
    ClassB和ClassA中的this是同一个 所以一定要套用面向对象的逻辑来说你的ClassB覆盖了ClassA的属性name
      

  2.   


    function ClassA(sColor){
        this.name = sColor;
        this.sayColor = function(){
                alert(this.name);
        }
    }
    function ClassB(sColor,sName){
            this.newMothod = ClassA;    // name=undefined
            this.newMothod(sColor);     // name=red
            delete this.newMothod;      // name不变
            this.name = sName;          // name=green !!!!! 注意:这里开始name变了
            this.showName = function(){ // 这里的showName其实和sayColor是一样的都是alert这个name
                    alert(this.name);
            }
    }
    var objA = new ClassA("blue");
    var objB = new ClassB("red","green");
    objA.sayColor();
    objB.sayColor();
    objB.showName();