Wednesday 15 July 2015

JavaScript Classes and Variable Scope -


I am relatively new to JS and I have issues imitating OOP principles properly I

< P> I think I have two questions, the first question is that there are several ways to declare variables.

I have a class:

  function clas (a) {this.b = 2; Var c = 3; This.prototype.d = 4; // or clazz.prototype.d = 4? } Var myClazz = new clause (1);  

Am I correct in the following assessments:

A is a personal variable which is a specific example (i.e. in different instances of clause there will be unique and independent variables 'a' ). It can be accessed from within the claza: 'A'.

b is a public variable, which is special for example, it can be accessed in the form of 'this.b' and 'cleage' from within the clazes as 'macaseb.b'.

c is a personal variable that is stable, or class-specific (i.e., different examples of clauses are part of the same 'C' variable). From any instance of the claage can be entered as 'C' and changes in the example of clause are reflected in all the examples of the clause.

d is a public variable that is static / can be accessed from anywhere through 'claz.propotype D' or 'myclage.prototype D'.

I have a total issue with my understanding of the variable scheme that a personal variable that is not static (i.e., a unique version for each instance of the class).

The second question is in relation to different types of class declarations.

I have been using:

  var MySingleton = new function () {...};  

Is it right to create aggregation? I am also unsure of the effect of "new" keywords in this situation, along with the effect of the aping () function at the end of the declaration:

  var MySingleton = new function () {... } ();  

I am using this method to declare a class and then instantiate the examples of that class:

  function myClass () {...}; Var class A = new myClass (); Var classB = new myClass ();  

Is this the proper way?

You are right one and b :

A is an argument, which is only available within the scope.

b is a public example variable, which is available on all instances created with the constructor function.

c is a private variable, which is only accessible within the constructor function.

d declaration is invalid, because the prototype object is used only for use, such as Clazz.prototype D = 3; , if you do this, the variable will be shared, but you can specify a value at a particular frequency, and the default value shading (via prototype series).

You can use the c form for the "private variable", for example:

  function Clazz () {var c = 3; // Private variable this.privilegedMethod = function () {warning (C); }; }  

The privileged methods are public, but they can use the declared "private" variable inside the constructor function.

To create aggregation, the easiest way to possibly use an object verbatim, such as:

  var myInstance = {method1: function ()} {//. ..}, method2: function () {// ...}};  

And if you want a personal member on your singleton instance, you can do the following:

  var myInstance = (function () {var privateavar = ''; Function PrivateMethod () {// ...} return {// public interface publicMethod1: function () {// all private members are available here}, publicMethod2: function () {}};}) ;  

This is called module pattern, it basically allows you to use private members on any one object.

More information:

  • Edit: About the syntax you posted:

      var mySingleton = new (function () {// ...}) (); By using the   

    new operator, you declare and use one step in an " anonymous constructor function ", Which will generate a new object instance, it is valid, but I personally like the" module "pattern approach, to avoid my own object insensible (and new ) for).

    Additionally, reading new function () {} , I think it is not really spontaneous and can cause confusion, if you do not understand well It has been that the new operator works.

    Regarding brackets, they are optional, if the new operator will call the function constructor without the parameter if you do not add them ( see, 11.2.2 < / Em>).


    No comments:

    Post a Comment