Jump to content

Why declare 'new'?


lordvader

Recommended Posts

I've just started learning js by reading Learning JavaScript by Shelley Powers. It's okay I guess... I'm through 4 chapters.

Anyways, there's this explanation of declaring new instances of objects. My question is, what's that for?

Chapter 4.1:

 

var holdAnswer = new Boolean(true);

 

This is the only line of the example... so isn't holdAnswer already a new variable?

So why use 'new'? The book only says you can do it, but not why.

Thanks

Link to comment
https://forums.phpfreaks.com/topic/103614-why-declare-new/
Share on other sites

But why do i need to use 'new' to create an object or set the type of variable?

What is the difference between

 

var first = array()

 

and

 

var first = new array()

 

You can call Array() as a predefined class of javascript.

 

var first = new Array();

 

It means you a creating a new instance of the class Array.

 

Link to comment
https://forums.phpfreaks.com/topic/103614-why-declare-new/#findComment-530882
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.