5

我有以下 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);

            });
  1. 我的对象构造方法是一个好习惯吗?

  2. undefined在尝试访问时进入了 helperFunc this.prop3

  3. 我也尝试分配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();

..我得到this.getTotal的不是函数

我一直在阅读对象创建和使用闭包来模仿私有成员等等,因为没有一种方法可以定义对象,所以我感到困惑。

TBH - 我不太了解这个结构:

                       var myObject = (function() { ... } ();

我已经看到它在 jQuery 插件中使用了很多,但是最后的第一个括号后面是空括号是什么意思?

任何传授的知识将不胜感激。

另外,我已经订购了 Douglas Crockford 关于 javascript 的书,在它到达之前我需要尝试解决这个问题

4

4 回答 4

4

引用他提到的 Xhalent 的精彩文章(写得非常好,文笔清晰):

这是因为“this”的值与创建对象时“this”的值不同。

所以在你的情况下:

...
  var _this = this.prop3;
  function helperFunc() {
    return _this;
  }
...

可能会达到预期的效果。

于 2011-05-17T12:59:09.543 回答
1

我已经完成了一系列关于基本 javascript 基础的系列文章——这里介绍了对象和原型:

Javascript 对象实例化和原型

我在这里深入研究闭包:Javascript闭包

希望他们有所帮助。干杯。

于 2011-05-17T12:34:28.330 回答
1

如果你有一个使用 的函数this,你必须确保调用上下文是正确的。即使用this.helperFunc(),而不仅仅是helperFunc()(但您还需要进行设置以便定义 this.helperFunc)。this您的示例中的内部 helperFunc的所指对象与Create().

你可以把它想象成函数没有被定义为对象的成员,而是被称为对象的成员。

this根据上下文,可能会解决三件事。

  1. 一个新创建的对象,如果函数调用前面有new关键字。
  2. 调用函数时点左侧的对象。
  3. 全局对象 ( window),如果以上都没有提供。

如果在没有对象的情况下调用函数,例如在调用 in 的情况下helperFuncthis.Createthis绑定到全局对象(window在浏览器中使用时)

鉴于这样的事情:

var o = {someVal:"hello"};
o.doSomething = function (){alert(this.someVal)};

呼叫o.doSomething()显然会提示“你好”。

鉴于:

var o2  = {someVal:"world"};
o2.someFunc = o.doSomething;

调用o2.someFunc()会提示“world”,而不是“hello”,如果它是指向 doSomething.o

并给出:

var someFunc = o.doSomething
someVal = "sailor"

呼叫someFunc()会提醒“水手”。

另一个混淆点是this直接使用 within Setup()。当您使用 调用函数时new,正如您所做的那样,this它不会绑定到全局对象,而是绑定到对象的新实例Setup

对于我上面的示例,这意味着调用new o.doSomething()将警告“未定义”,因为为调用创建的新对象没有“someVal”成员。

于 2011-05-17T13:14:33.233 回答
0

您在此页面上有一个由 Mike Koss 编写的关于 JS 中的 OOP 的很好的教程。

我已经看到它在 jQuery 插件中使用了很多,但是最后的第一个括号后面是空括号是什么意思?

第二组括号立即调用您在第一组括号中声明的函数。

您可以声明它并单独执行它(允许多次执行):

var myFunction = function() { alert("boo"); }; // Declare & instanciate
myFunction(); // Invoke
myFunction(); // Invoke again

或者在一行中同时执行:

(function() { alert("boo"); })(); // Can only invoke now, the function is anonymous and not stored anywhere.
于 2011-05-17T12:23:59.123 回答