私は次のjavascriptを持っています:
var MyObject = (function() {
function Setup(args) {
this.prop1 = args.x;
this.prop2 = args.y
this.prop3 = this.prop1 + this.prop2;
this.Create = function() {
return 'a' + helperFunc();
}
function helperFunc() {
return this.prop3;
}
}
return {
init : function(args) {
var setup = new Setup(args);
setup.Create();
}
}
})();
$(function() {
MyObject.init(someArgs);
});
オブジェクト構築への私のアプローチは良い習慣ですか?
に
undefined
アクセスしようとすると、helperFunc に入りますthis.prop3
。this.prop1 + this.prop2
また、ローカル変数に割り当て、関数を使用してこの値を次のように返すことも試みました。function Setup(args) { var total; this.prop1 = args.x; this.prop2 = args.y total = this.prop1 + this.prop2; this.getTotal = function() { return total; }; this.prop3 = this.prop1 + this.prop2; ...
...そして、これを helperFunc で次のように呼び出すと:
return this.getTotal();
.. getthis.getTotal
は関数ではありません
オブジェクトの作成について読んだり、クロージャーを使用してプライベートメンバーを模倣したりしていますが、オブジェクトを定義する方法が1つもないため、混乱しています。
TBH - 構文がよくわかりません:
var myObject = (function() { ... } ();
jQuery プラグインでよく使われているのを見てきましたが、最初の括弧の後に空の括弧が続くのは何を意味し、何をするのでしょうか?
与えられた知識は大歓迎です。
また、JavaScript に関する Douglas Crockford の本を注文していますが、それが届くまで、この問題を解決する必要があります。