【问题标题】:How to call a function during object construction in Javascript?如何在 Javascript 中的对象构造期间调用函数?
【发布时间】:2011-06-15 00:44:57
【问题描述】:

我想创建一个对象并在创建对象时运行它的两个方法。所以如果我的对象是

function newObj(){
this.v1 = 10;
this.v2 = 20;
this.func1 = function(){ ....};
this.func2 = function(){...};
}

并且对对象的调用是

var temp = new newObj();

我想运行 func1()func2() 而不在临时变量上显式调用它们,例如 temp.func1()。我希望在创建新的 Object 变量时调用它们。我尝试将this.func1() 放在newObj 声明中,但它似乎不起作用。

【问题讨论】:

    标签: javascript function object call


    【解决方案1】:

    在构造函数中添加方法调用语句:

    函数 newObj(){ 这个.v1 = 10; 这个.v2 = 20; this.func1 = function(){ ....}; this.func2 = function(){...}; this.func1(); this.func2(); }

    我认为这是您需要的解决方案。

    【讨论】:

      【解决方案2】:

      只需从构造函数本身调用它就可以了:http://jsfiddle.net/yahavbr/tTf9d/

      代码是:

      function newObj(){
          this.v1 = 10;
          this.v2 = 20;
          this.func1 = function() { alert("func1"); };
          this.func2 = function() { alert("func2"); };
      
          this.func1();
          this.func2();
      }
      

      【讨论】:

        【解决方案3】:

        这在 Chrome 中适用于我:

        function newObj(){
          this.v1 = 10;
          this.v2 = 20;
          this.func1 = function(){ this.v1 += 1; };
          this.func2 = function(){ alert(this.v1); };
          this.func1();
          this.func2();
        }
        var obj = new newObj();
        

        【讨论】:

          【解决方案4】:

          如果您从不打算重用它,请尝试将其包装在自调用函数中,如下所示:

          function newObj(){
              this.v1 = 10;
              this.v2 = 20;
              this.func1val = (function(){ alert('called from c\'tor'); })();
              this.func2val = (function(){ return 2 + 1; })();
          }
          
          var temp = new newObj();
          alert('temp.func2val = ' + temp.func2val);
          

          DEMO

          【讨论】:

          • 谢谢,这行得通。 this.func1() 对我来说似乎在 Chrome 中不起作用。
          • 函数 highLightElement(element) { this.LEVEL = 128; this.ELEMENT = 元素; this.INTERVAL = 100; this.setTimeout(increaseYellow, INTERVAL); this.setYellowLevel = (function() { var hex = this.LEVEL.toString(16); element.style.backgroundColor = '#ffff' + hex; })(); this.newfunc = (function(){ setTimeout(increaseYellow, INTERVAL);})(); this.increaseYellow = function(){ LEVEL += 10;如果(级别> 255){级别= 255; } setYellowLevel(ELEMENT, LEVEL); if (LEVEL
          【解决方案5】:

          使用我们可以调用的自调用函数,我们还可以通过围绕公共变量var that = this;做一些工作来共享父参数

          function newObj(){
          
          this.v1 = 10; // public variable
          this.v2 = 20; // public variable
          var that = this;  // Allow access to parent function variable to inner function
             (function(){
               // access parent function variable
               // using 'that' ex: 
                 that.v1 = 50;
               //fun1code stuff 
             })();
          
             (function(){
               // access parent function variable
               // using 'that' ex: 
               that.v2 = 60;
               //fun2code stuff 
             })();
          }
          
          var temp = new newObj();
          console.log(temp.v1);  // output 50
          console.log(temp.v2);  // output 60
          

          【讨论】:

            【解决方案6】:

            我认为可能需要强调的是,在 JavaScript 中,您需要定义对象的函数(或方法,如果您更喜欢该术语)在调用它们之前

            例如,如果您想在实例化时调用this.func1()

            var new_object = new newObj();  // create/instantiate an object
            
            function newObj(){
              this.v1 = 10;
              this.v2 = 20;
            
              this.func1();  //  <-- calling it here causes an error
            
              this.func1 = function(){ ....};
              this.func2 = function(){ ....};
              this.func3 = function(){ ....};
            }
            

            TypeError: this.func1 不是函数

            这是我多年前在尝试了解如何在 JS 中进行 OOP 时遇到的一个问题。因为在 Java 或 PHP 等其他语言中,您通常在类的顶部有一个构造函数/方法,然后在您的其他函数/方法中编写下方。

            因此,这样编写类似乎是合乎逻辑的:1) 定义对象的属性,然后 2) 列出实例化对象时要执行的操作,然后 3) 列出其他类函数/方法。

            但是没有!!

            使用 JavaScript,您必须在调用对象函数之前定义它们

            因此,如果您想在对象创建/实例化时调用两个方法,比如说this.func1()this.func2(),首先在您的类中定义所有内容并在最后 放置您的方法调用:

            var new_object = new newObj();  // create/instantiate an object
            
            function newObj(){
              this.v1 = 10;
              this.v2 = 20;
            
              this.func1 = function(){ ....};
              this.func2 = function(){ ....};
              this.func3 = function(){ ....};
            
              this.func1();  //  <-- it works here!
              this.func2();  //  <-- it works here!
            }
            

            如果你想让你的代码用构造方法组织在其他类方法的顶部(如前所述,PHP 和 Java 是如何做到的),那么你可以创建一个小this._constructor() 方法并将东西放在那里,并在课程结束时调用它:

            function newObj(){
              this.v1 = 10;
              this.v2 = 20;
            
              this._constructor = function(){  // do constructor things here
                this.func1();
                this.func2();
              }
              this.func1 = function(){ ....};
              this.func2 = function(){ ....};
              this.func3 = function(){ ....};
            
              this._constructor();  // call just one method here, nice and tidy
            }
            

            有些人可能会说这有点多余,但是任何有助于加快您的工作流程的方法... :)

            为了记录,如果你想在创建/实例化一个对象时传递一些参数,说你想有设置this.v1的选项,那么你可以这样做:

            function newObj(set_v1){
              this.v1 = 10;
              this.v2 = 20;
            
              this._constructor = function(set_v1){  // do constructor things here
                if ( set_v1 != undefined ){  // you can come up with a better condition here
                  this.v1 = set_v1;
                }
                this.func1();
                this.func2();
              }
              this.func1 = function(){ ....};
              this.func2 = function(){ ....};
              this.func3 = function(){ ....};
            
              this._constructor(set_v1);  // call the constructor here and pass the argument
            }
            

            【讨论】:

              猜你喜欢
              • 2018-12-21
              • 2012-09-21
              • 1970-01-01
              • 2015-07-11
              • 1970-01-01
              • 2013-09-23
              • 2011-12-15
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多