简体   繁体   中英

oops in javascript : call one method from another?

I am trying to manage my code in javascript.What I have done is created two classes.

(function(){
 Main1 = function(){
    return this;
  }

Main1.prototype = {
Main1_method1 : function(){

},
Main1_method2 : function(){

}
}


})();

(function(){
 Main2 =function(){
        return this;
      }

    Main2.prototype = {
    Main2_method1 : function(){

    },
    Main2_method2 : function(){

    }
    }
})();

var MyInstance1 = new Main1();
var MyInstance2 = new Main2();

Question : I want to invoke one method in another.I want to call Main2_method1 in Main1_method1 ,but i have no idea how to do it.

I can use the classical model( function.prototype.method = function(){}) or the prototypal model( object.create ).But I want to do this using the above method.

Well, you'll need to call the method on the instance:

(function(){
    Main2 =function( instance1 ) {
        this.main1 = instance1;
        return this;
     }

    Main2.prototype = {
        Main2_method1 : function() {
            this.main1.Main1_method1();
        }
    }
})();

var MyInstance1 = new Main1();
var MyInstance2 = new Main2( MyInstance1 );

The only part you need to figure out is how Main2 access the Main1 instance. You can simply put it globally which will work.

But to be clean, you'd probably want to inject Main1 instance inside Main2 on instantiation. Or, you keep them decoupled using an event hub.

I'm not sure that understood what you need and not sure that you understand it yourself ))

But if I understood you correctly... ))

(function(){

    Main1 = function(){return this};

    Main1.prototype = {
        method1 : function(){this.method1()}
    };

}());

(function(){

    Main2 = function(){return this};

    Main2.prototype = {
        method1 : function(){console.log('Eh?')}
    };

}());

var MyInstance1 = new Main1();
var MyInstance2 = new Main2();

MyInstance1.method1.call(MyInstance2);

Inspired in the book " Javascript Patterns" by Stoyan Stefanov , you can use the "Public static Members" pattern, which allows you to define some special methods for an object accessible from everywhere. See that example:

    // Define a first object
    var Main1 = function(){
      var method1 = function() {
        return 'Main1.Method1';
      }

      var method2 = function(){
        return "I'm Main1 method2, executing: " + Main2.Method1();
      }

      return {
        Method1 : method1,
        Method2 : method2
      }
    }

    // Define a second object
    var Main2 = function(){
      var method2 = function() {
        return 'Main2.Method1';
      }
      return {
        Method2 : method2
      }
    }
    // Add a static method to Main2
    Main2.Method1 = function(){
      return 'Main2.Method1';
    }

    // Then you can execute the static method without 
    // instantiate its object...
    console.log(Main2.Method1()); // output: Main2.Method1

    // So, this will work
    var foo = new Main1();
    console.log(foo.Method2()); //output: I'm Main2 Method2, executing: Main2.Method1

There is not the only one solution, but in my opinion it is one of the best.

Back in your code, perhaps the problem is that you have defined the two objects in separate closures. So Main1 cannot use Main2 methods because of Main2 have been defined in a diferent scope.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM