oop - Javascript Class properties and methods work differently -


i have been working javascript while , used notation create objects

var classname = (function() {   var property = 1;  //example of property    var method = function () {       //example of method     }; }); 

but have project use angularjs , javascript don't recognize it. can use 1

var classname = (function() {   this.property = 1;  //example of property    this.method = function() {     //example of method   };  }); 

is there reason first 1 not working? in experience prefer first 1 better

[edit]

var classname = (function() {   var property = 1;  //example of property    var method = function () {       //example of method     }; })(); 

var classname = (function() {   var property = 1;  //example of property    var method = function () {       //example of method     }; }); 

in example you're not creating surmounts property and/or method. you're assigning function variable classname, , within function you're creating 2 more variables. variables function-scoped, , definition, won't visible outside function.


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -