jquery - Javascript Difference between normal function and function object -


In JavaScript, the way to create classes (or objects) is to use;

  function MyOn () {this.somevar = "xyz"; This is a common task ... how different is this function from a normal javascript function ... a function that adds 2 numbers?   

The function is no different. For example, they have similar effects:

  function myob () {this.somevar = "xyz"; This.someMethod = function () {console.log (this.someVar); }} Var obj = new MyObj (); Obj.someMethod ();   

and

  function someMethod () {console.log (this.someVar); } Function myob () {this.somevar = "xyz"; } Var obj = new MyObj (); SomeMethod.call (obj);   

and

  function someMethod () {console.log (this.someVar); } Function myob () {this.somevar = "xyz"; } Var obj = new MyObj (); Obj.f = someMethod; Obj.f ();   

As you have tagged your question, I will finish by saying that the best way to make your function will be:

  Function MyOne ( } {This.somevar = "xyz"; } MyObj.prototype.someMethod = function () {console.log (this.someVar); } Var obj = new MyObj (); Obj.someMethod ();   

In this way, all the examples of MyObj do the same work and are lighter in this way.

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -