[英]How to call a function during object construction in Javascript?
I want to create an object and run two of its methods on object creation. 我想创建一个对象,并在创建对象时运行其两个方法。 So if my object is
所以如果我的对象是
function newObj(){
this.v1 = 10;
this.v2 = 20;
this.func1 = function(){ ....};
this.func2 = function(){...};
}
and the call to the object is 对该对象的调用是
var temp = new newObj();
I want to run func1()
and func2()
without calling them explicity on temp variable, like temp.func1()
. 我想运行
func1()
和func2()
而不在temp变量(例如temp.func1()
上显式调用它们。 I want them to be called when I create the new Object variable. 我希望在创建新的Object变量时调用它们。 I tried putting
this.func1()
inside the newObj
declaration but it doesn't seem to work. 我尝试将
this.func1()
放入newObj
声明中,但它似乎不起作用。
Add method invocation statements in constructor: 在构造函数中添加方法调用语句:
function newObj(){ this.v1 = 10; this.v2 = 20; this.func1 = function(){ ....}; this.func2 = function(){...};this.func1(); this.func2(); }
I think it is solution of your needs. 我认为这是您需求的解决方案。
Just call it from within the constructor itself it works just fine: http://jsfiddle.net/yahavbr/tTf9d/ 只需从构造函数本身中调用它就可以了: http : //jsfiddle.net/yahavbr/tTf9d/
The code is: 代码是:
function newObj(){
this.v1 = 10;
this.v2 = 20;
this.func1 = function() { alert("func1"); };
this.func2 = function() { alert("func2"); };
this.func1();
this.func2();
}
This works for me in Chrome: 这在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();
Try wrapping it in an self invoking function if you never plan on reusing it, like this: 如果您从不打算重用它,请尝试将其包装在自调用函数中,如下所示:
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);
Using Self invoking function we can call and we can also share parent parameter by doing some work around public variable var that = this;
使用自调用功能,我们可以调用,也可以通过围绕公共变量
var that = this;
进行一些工作来共享父参数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
I think perhaps it needs to be stresed that in JavaScript you need to define the object's functions (or methods, if you prefer that term) before you call them . 我想也许它需要stresed是在JavaScript中,你需要定义对象的函数 (或方法,如果你喜欢这个词), 你给他们打电话了 。
For example, if you want to call this.func1()
upon instantiation: 例如,如果要在实例化时调用
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 is not a function
TypeError:this.func1不是函数
This is a problem I came across years ago when trying to understand how to do OOP in JS. 这是我几年前在尝试了解如何在JS中进行OOP时遇到的一个问题。 Because in other languages like Java or PHP, you have a constructor function/method usually at the top of your class, and beneath you write in your other functions/methods.
因为在其他语言(如Java或PHP)中,通常在类的顶部,而在其下方,则使用其他函数/方法编写构造函数/方法。
So it would seem logical to write your class thus: 1) define your object's properties, and then 2) list the things you want to do when the object is instantiated, and then 3) list the other class functions/methods. 因此,编写这样的类似乎是合乎逻辑的:1)定义对象的属性,然后2)列出实例化对象时要执行的操作,然后3)列出其他类的函数/方法。
BUT NO!! 但不是!!
With JavaScript, you must define the object's functions before you call them . 使用JavaScript,必须先定义对象的函数,然后才能调用它们 。
So if you want to call two methods on object creation/instantiation, lets say this.func1()
and this.func2()
, first define everything in your class and at the end place your method calls: 因此,如果要在对象创建/实例化上调用两个方法,可以说
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!
}
If you wanted to have your code organised with a constructor method placed at the top of other class methods (like previously mentioned, how PHP and Java do it) then you could make a little this._constructor()
method and place things there, and call it at the end of your class: 如果您想将代码与其他类方法的顶部构造一个构造函数方法(如前所述,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
}
Some may say it's kinda redundant, but whatever helps to make your workflow faster... :) 有人可能会说这有点多余,但是可以帮助您加快工作流程的任何事情... :)
Just for the record, if you want to pass some argument when creating/instantiating an object, say you wanted to have the option to set this.v1
then you could do it like this: 仅作记录,如果您想在创建/实例化对象时传递一些参数,说您想选择设置
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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.