【问题标题】:what is wrong with this piece of code of javascript inheritance?这段javascript继承代码有什么问题?
【发布时间】:2012-10-09 09:34:02
【问题描述】:
function condition(){ 
  this.expression = ""; 
  this.toString = function(){
    return this.expression;
  }
};

function and(first, second){
    this.expression = first + " and " + second;
}

function nop(){};
nop.prototype = condition.prototype;

and.prototype = new nop();

var a =new and(1,2);

console.log(a.toString());

预计会看到“1 和 2”作为输出,但实际情况如下: “[对象对象]”

【问题讨论】:

    标签: javascript oop inheritance


    【解决方案1】:

    您正在将condition 的原型转移到nop 的原型。问题是您的condition.toString 没有在原型中声明...这里:

    function condition(){ 
      this.expression = ""; 
    
    };
      condition.prototype.toString = function(){
        return this.expression;
      }
    function and(first, second){
        this.expression = first + " and " + second;
    }
    
    function nop(){};
    nop.prototype = condition.prototype;
    
    and.prototype = new nop();
    
    var a =new and(1,2);
    
    console.log(a.toString());
    

    function condition(){ 
      this.expression = ""; 
      this.toString = function(){
        return this.expression;
      }
    };
    
    function and(first, second){
        this.expression = first + " and " + second;
    }
    
    function nop(){};
    nop = condition;
    
    and.prototype = new nop();
    
    var a =new and(1,2);
    
    console.log(a.toString());
    

    【讨论】:

      【解决方案2】:

      你没有重写 toString 方法,因为条件的构造函数永远不会被调用!尝试这样做;

      condition.prototype.toString=function(){
          return this.expression;
      }
      

      【讨论】:

        【解决方案3】:

        尝试将字符串传递给您的 and 函数,因为此时您正尝试将整数连接到字符串 var a =new and("1","2");

        【讨论】:

          【解决方案4】:

          应该是这样的

          function condition(){ 
            this.expression = ""; 
          };    
          
          condition.prototype.toString = function(){
             return this.expression;
          }
          

          【讨论】:

            【解决方案5】:

            好的,所以这里的问题是您将两种继承模式 (http://davidshariff.com/blog/javascript-inheritance-patterns/) 伪经典与功能模式混合在一起。

            您可以通过在构造函数上添加方法来创建对象:

            function MyClass() {
                var privateProperty = 1;
                this.publicProperty = 2;
            
                function pivateMethod() {
                    // some code ...
                }
            
                this.publicMethod = function() {
                    // some code ...
                };
            }
            
            // inheritance
            function SubClass() {
                MyClass.call(this);
            
                this.newMethod = function() { };
            }
            

            在这里,当您创建此类的实例时,您将再次创建每个方法。

            那么你就有了原型模式:

            function MyClass() {
                this._protectedProperty = 1;
                this.publicProperty = 2;
            }
            MyClass.prototype._protectedMethod = function() {
                // some code ...
            };
            MyClass.prototype.publicMethod = function() {
                // some code ...
            };
            
            // inheritance
            function SubClass() {
                MyClass.call(this);
            }
            SubClass.prototype = new MyClass();
            SubClass.prototype.newMethod = function() { };
            
            // OR
            function SubClass() {
                MyClass.call(this);
            }
            
            function dummy() { }
            dummy.prototype = MyClass.prototype;
            SubClass.prototype = new dummy();
            SubClass.prototype.newMethod = function() { };
            

            如果你必须选择这两种模式中的一种,而不是两者兼而有之·

            我已经在这个小提琴上修复了你的代码:http://jsfiddle.net/dz6Ch/

            【讨论】:

              猜你喜欢
              • 2022-11-16
              • 2016-10-05
              • 2013-04-11
              • 2011-06-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多