JavaScript Using “.call” to create a new JavaScript object instead of “new”

I was trying the below commands on Chrome console. I am able to create the object using new(line 2 below) but using call doesn't work. Can anyone explain what could be the reason ?

function ObjConstructor(){ this.sample = 1};

let withNew = new ObjConstructor();

let usingCall = ObjConstructor.call({});

usingCall
undefined  //output that came on console, this is not a command

withNew
ObjConstructor {sample: 1} //output that came on console
Answer:1

new does several things including:

  • Creating an object
  • Setting the this value to that object
  • Causes the function to return that object by default

Your code:

  • Creates an object manually with {}
  • Sets the this value to that object with call()

… but doesn't do the last thing. There is no return statement in the function, so it returns undefined.

Answer:2

The result of call is whatever the function returns. Your ObjConstructor doesn't return anything, so the result of calling it is undefined.

In contrast, when you use new, a new object is created and passed to the function, and unless the function returns a non-null object, the object created for new is the result of the new expression.

That's why the new version works but the call doesn't.

Also note that call doesn't create an object at all. In your ObjConstructor.call({}), what creates the object is {}, not call. It won't have ObjConstructor.prototype as its prototype. ({} is a raw object initializer, so the object will have Object.prototype as its prototype.)

Answer:3

I have the following method to paste some HTML at the caret position, using `range.insertNode(), sourced from this SO answer function insertAtCursor(html) { var sel, range; if (window....

I have the following method to paste some HTML at the caret position, using `range.insertNode(), sourced from this SO answer function insertAtCursor(html) { var sel, range; if (window....

  1. paste html at caret

I mostly see JavaScript use classes as a constructor as following: class Rectangle { constructor(height, width) { this.height = height; this.width = width; } // Getter get area() { ...

I mostly see JavaScript use classes as a constructor as following: class Rectangle { constructor(height, width) { this.height = height; this.width = width; } // Getter get area() { ...

  1. what is a national emergency
  2. whatsapp
  3. whataburger
  4. what time is it
  5. what is my ip
  6. what song is this
  7. what is a state of emergency
  8. what is a dry cough
  9. what happens in a national emergency
  10. what is the stafford act
  11. what is a bear market
  12. what does ���� mean
  13. what to watch
  14. what stocks to buy today
  15. whataburger menu
  16. what is the weather
  17. what is rsv
  18. what is gluten
  19. what is pv
  20. what is a boomer

I'm creating a web using Bootstrap 3 and I'm using the page-content-wrapper script. When I collapse the page, logo of my web(left right corner in navbar) changes the position due to the padding that ...

I'm creating a web using Bootstrap 3 and I'm using the page-content-wrapper script. When I collapse the page, logo of my web(left right corner in navbar) changes the position due to the padding that ...

I have the following JavaScript object: var example = [{ country: "US", things: { weather: 'cloudy' } }, { country: "US", things: { resource: 'lead', weather:...

I have the following JavaScript object: var example = [{ country: "US", things: { weather: 'cloudy' } }, { country: "US", things: { resource: 'lead', weather:...