Call Javascript Factory Method

I am learning Object Oriented Java Script. I have below code for Factory method.

function Foo() { var value = 1; return { method: function() { return value; }, value:value } } Foo.prototype = { bar: function() {} }; new Foo(); Foo(); 

The Foo method can be called in two ways. new Foo(); or Foo(); Both do the same, and the conclusion is the same. what is the actual difference in java script processing?

+4
source share
4 answers

In normal cases, new should be used when creating objects from the constructor function and avoided when making any other function call. When using new for function 1) a new object is created; 2) the function is called with the this value associated with this new object, and 3) the value returned by the function (default) is the object created in the first step.

However, in your case, you are returning a completely new object from the "constructor" (different from the object in 1) above), which means that there is no practical difference. The this value will still differ inside the function, but both of them will return false :

 new Foo() instanceof Foo; Foo() instanceof Foo; 

To illustrate this difference, add the following to Foo :

 alert(this instanceof Foo) 

When called with new this warns true ; if called without it, false .

Also, it makes no sense to assign a Foo.prototype object because you will never create instances of Foo that could use it (because, again, you are returning something completely different from Foo ).

If you intend to return a custom object from Foo , you should prefer the version without new ; it makes no sense to create a new instance of Foo if you ignore it and return something completely different one way or another.

+3
source

Here's a great link:

Is javascript "new" considered harmful?

See also:

http://phabricator.com/docs/phabricator/article/Javascript_Pitfalls.html

But here is the best link (and indeed the answer to your question:

http://www.crockford.com/javascript/inheritance.html

JavaScript is a class-free, object-oriented language, and as such it uses prototype inheritance instead of classical inheritance. This can be a mystery for programmers studying in ordinary object-oriented languages ​​such as C ++ and Java ...

+2
source

The new javascript keyword in functions is rather strange. Best (only?) A detailed explanation of what is happening, I was able to actually find this SO answer from Daniel Howard.

Reading should be required for those trying to learn some of the more advanced concepts of Javavscript.

By the way, you can get really far in js by never using a new keyword. Note that in principle, none of them use jquery.

+1
source

Since you are returning an object from Foo , using new not affected. The return values ​​of Foo() and new Foo() will not be instances of Foo ; instead, they will be instances of the type of any return value.

The same behavior is observed if you return any object or any function from your constructor. However , if Foo was to return:

  • Line
  • Number
  • null
  • undefined (same as returning nothing at all)
  • or this

... then the return value of new Foo() will be an instance of Foo .

You can check the code below on JSFiddle :

 function ReturnsString() { return "string"; } function ReturnsNumber() { return 1; } function ReturnsNull() { return null; } function ReturnsUndefined() { return void 0; } function ReturnsThis() { return this; } function ReturnsFunction() { return function() {}; } function ReturnsObject() { return {}; } alert("Results:" + "\nReturnsString: " + (new ReturnsString() instanceof ReturnsString) + "\nReturnsNumber: " + (new ReturnsNumber() instanceof ReturnsNumber) + "\nReturnsNull: " + (new ReturnsNull() instanceof ReturnsNull) + "\nReturnsUndefined: " + (new ReturnsUndefined() instanceof ReturnsUndefined) + "\nReturnsThis: " + (new ReturnsThis() instanceof ReturnsThis) + "\nReturnsFunction: " + (new ReturnsFunction() instanceof ReturnsFunction) + "\nReturnsObject: " + (new ReturnsObject() instanceof ReturnsObject) ); 

The ReturnsFunction and ReturnsObject will show the result false , that is, they do not return instanceof themselves when used with the new operator.

All other do constructors return instanceof themselves when used with the new operator.

† Except for this , as I said above

0
source

Source: https://habr.com/ru/post/1389012/


All Articles