我想创建一个对象并在对象创建时运行它的两个方法。所以如果我的对象是
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
声明中,但它似乎不起作用。
答案 0 :(得分:10)
在构造函数中添加方法调用语句:
function newObj(){ this.v1 = 10; this.v2 = 20; this.func1 = function(){ ....}; this.func2 = function(){...}; this.func1(); this.func2(); }
我认为这是您需求的解决方案。
答案 1 :(得分:6)
只需从构造函数本身调用它就可以正常工作: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();
}
答案 2 :(得分:4)
这适用于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();
答案 3 :(得分:1)
如果您从未计划重复使用它,请尝试将其包装在自动调用函数中,如下所示:
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 强>
答案 4 :(得分:0)
使用自我调用函数我们可以调用,我们也可以通过对公共变量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
答案 5 :(得分:0)
我想也许需要强调,在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
}