在Java中你可以调用方法来帮助你在构造函数中做一些繁重的工作,但javascript需要首先定义方法,所以我想知道是否有另一种方法可以解决这个问题或者我是否被迫在定义之后调用执行重物的方法。我更喜欢保持Object / Class中包含的实例函数,并且我必须在对象/类的最末端拥有构造函数,这感觉很奇怪。
function Polynomials(polyString)
{
// instance variables
this.polys = [];
this.left = undefined;
this.right = undefined;
// This does not work because it's not yet declared
this.parseInit(polyString);
// this parses out a string and initializes this.left and this.right
this.parseInit = function(polyString)
{
//Lots of heavy lifting here (many lines of code)
}
// A lot more instance functions defined down here (even more lines of code)
// Is my only option to call it here?
}
答案 0 :(得分:3)
这就是我要做的事情:
var Polynomials = function() {
// let's use a self invoking anonymous function
// so that we can define var / function without polluting namespace
// idea is to build the class then return it, while taking advantage
// of a local scope.
// constructor definition
function Polynomials( value1) (
this.property1 = value1;
instanceCount++;
// here you can use publicMethod1 or parseInit
}
// define all the public methods of your class on its prototype.
Polynomials.prototype = {
publicMethod1 : function() { /* parseInit()... */ },
getInstanceCount : function() ( return instanceCount; }
}
// you can define functions that won't pollute namespace here
// those are functions private to the class (that can't be accessed by inheriting classes)
function parseInit() {
}
// you can define also vars private to the class
// most obvious example is instance count.
var instanceCount = 0;
// return the class-function just built;
return Polynomials;
}();
备注:
Rq 1:
原型函数是可用于每个类实例的公共方法。
var newInstance = new MyClass();
newInstance.functionDefinedOnPrototype(sameValue);
RQ2: 如果你想要真正的私人'变量,你必须这样:
function Constructor() {
var privateProperty=12;
this.functionUsingPrivateProperty = function() {
// here you can use privateProperrty, it's in scope
}
}
Constructor.prototype = {
// here define public methods that uses only public properties
publicMethod1 : function() {
// here privateProperty cannot be reached, it is out of scope.
}
}
就个人而言,我只使用属性(而不是私有变量),并使用' '通知财产的通用惯例是私人的。所以我可以在原型上定义每个公共方法 之后,任何使用前缀为' '必须承担他/她的责任,这似乎是公平的。 : - )
对于function fn() {}
和var fn= function() {}
之间的差异,google或S.O.对于这个问题,简短的回答是function fn() {}
获取函数定义并在整个范围内赋值,当var
得到var定义时,但它的值仅在代码运行评估时进行评估。
答案 1 :(得分:1)
您的实例变量'宣布在'这个'如果您正在寻找Java等价物的对象有点像将它们公之于众。您可以使用var
关键字声明变量,这使得它们更像构造函数中的私有变量。然后他们会受到“吊装”的影响。这基本上意味着它们被认为是在函数的顶部声明(或者声明它们的任何范围),即使你在调用代码之后编写它们也是如此。
答案 2 :(得分:0)
我会创建一个函数声明,然后将该变量赋值给函数声明。原因是JavaScript将hoist你的函数声明。
所以你可以这样做:
function Polynomials(polyString) {
// instance variables
this.polys = [];
this.left = undefined;
this.right = undefined;
// this parses out a string and initializes this.left and this.right
this.parseInit = parseInitFunc;
// This does not work because it's not yet declared
this.parseInit(polyString);
// A lot more instance functions defined down here (even more lines of code)
function parseInitFunc(polyString) {
console.log('executed');
}
// Is my only option to call it here?
}
这样你的代码就会保持干净。