Best practice for creating javascript objects

I have the following 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);

            });
  • Is my approach to building objects a good practice?

  • I get undefinedin helperFunc when trying to access this.prop3.

  • I also tried assigning to a this.prop1 + this.prop2local variable and using a function to return this value as follows:

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

... and when you call it in helperFunc as follows:

                       return this.getTotal();

.. i get is this.getTotalnot a function

I read around creating an object and used closure to emulate private members, etc., and since there is no way to identify objects that confuse me.

TBH - I really don't understand the construct:

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

I saw that he used a lot in jQuery plugins, but what does the first stage do, followed by an empty number at the end and mean?

.

, javascript, , .

+5
4

Xhalent ( ), :

, "this" "this", .

, :

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

.

+4

javascript - :

JavaScript

Closures : Javascript

, . .

+1

, this, , . .. this.helperFunc(), helperFunc() ( , .helperFunc ). this helperFunc , Create().

, , .

this .

  • , new.
  • .
  • (window), .

, helperFunc this.Create, this (window, )

- :

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

o.doSomething() "".

:

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

o2.someFunc() "", "", , doSomething o.

:

var someFunc = o.doSomething
someVal = "sailor"

someFunc() "".

- this Setup(). new, , this , Setup.

, new o.doSomething() "undefined", , , "someVal".

+1

JS .

, jQuery , ?

parenth , .

( ):

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

All Articles