2011-05-17 129 views
5

我有以下的javascript: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. 尝试访问this.prop3时,我在帮助功能中获得undefined

  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插件但随后空parenth什么第一parenth在到底是什么意思,做?

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

另外,我有秩序的道格拉斯Crockford的书JavaScript和直到它到达我需要尝试解决这个问题

+1

http://stackoverflow.com/questions/3921922/what-does-this-mean-function-x-y-a-b-in-javascript – mplungjan 2011-05-17 12:25:16

+0

这里有很多问题。如果你试图限制自己每个问题的一个问题,它会使得提供准确和精确的答案变得容易很多。 – 2011-05-17 12:32:04

+0

mplungjan - 这是理解函数调用结束时的父类的完美链接。我只需要了解为什么调用函数时调用属性而不是函数时会出现undefined ... – 2011-05-17 12:40:51

回答

4

Xhalent's wonderful article(真的做得很好,并明确wirtten)提到由他:

那是因为值“这” 为“本”,当创建 对象的值不同。

所以你的情况:

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

可能会达到什么样的期望的。

+0

这就是为什么我得到未定义的答案。我希望我能标记Paul Butchers的回答,因为这有助于理解问题。我读过Xhalent的文章,他们非常有用。将该值设置为一个私有变量我不知道我可以从helperFunc函数访问它。我花了太多时间在C#上,需要停止思考经典的OOP。期待Douglas Crockford的书。谢谢大家谁回答.... – 2011-05-17 13:32:12

0

您可以通过麦克科斯对OOP在JS上this page有一个很好的教程。

我已经看到它在jQuery中使用了大量 插件,但它随后空parenth什么第一 parenth在 到底是什么意思,做?

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

你可以宣布它和separatly执行它(允许多次执行):

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. 
1

如果您有一个使用this的函数,则必须确保调用上下文正确。即使用this.helperFunc(),而不仅仅是helperFunc()(但您还需要设置以便this.helperFunc被定义)。 this在您的示例中的helperFunc内的引用与Create()中的引用不同。

你可以把它看作虽然功能不是定义为对象的成员,但称为为对象的成员。

根据具体情况,this可能会解决这三个问题。

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

如果一个函数被调用无一物,如您在this.Create调用helperFunc的情况下,this将被绑定到全局对象(window,在浏览器中使用时)

鉴于东西像这样:

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

调用o.doSomething()将明显警报 “你好”。

考虑:

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

调用o2.someFunc()将提醒“世界”,而不是“你好”,就好像它是一个指向odoSomething会员,你期望的那样。

并考虑:

var someFunc = o.doSomething 
someVal = "sailor" 

调用someFunc()会提醒 “水手”。

另一个混乱点是this直接在Setup()内使用。当您使用new调用某个函数时,如您所做的那样,this未绑定到全局对象,而是绑定到Setup对象的新实例。

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