lordvader Posted April 30, 2008 Share Posted April 30, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/103614-why-declare-new/ Share on other sites More sharing options...
RichardRotterdam Posted April 30, 2008 Share Posted April 30, 2008 actually in javascript you dont have to do that you only say holdAnswer is of a type boolean you can do the same with arrays myArray=new Array(); but for a boolean isnt really needed Quote Link to comment https://forums.phpfreaks.com/topic/103614-why-declare-new/#findComment-530588 Share on other sites More sharing options...
lordvader Posted April 30, 2008 Author Share Posted April 30, 2008 Yes but what's the purpose of using 'new'? Quote Link to comment https://forums.phpfreaks.com/topic/103614-why-declare-new/#findComment-530599 Share on other sites More sharing options...
RichardRotterdam Posted May 1, 2008 Share Posted May 1, 2008 its either for creating an object or setting the type of a variable Quote Link to comment https://forums.phpfreaks.com/topic/103614-why-declare-new/#findComment-530765 Share on other sites More sharing options...
lordvader Posted May 1, 2008 Author Share Posted May 1, 2008 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() Quote Link to comment https://forums.phpfreaks.com/topic/103614-why-declare-new/#findComment-530817 Share on other sites More sharing options...
wrongmove18 Posted May 1, 2008 Share Posted May 1, 2008 Because you are instantiating the function or class to a variable. Quote Link to comment https://forums.phpfreaks.com/topic/103614-why-declare-new/#findComment-530881 Share on other sites More sharing options...
wrongmove18 Posted May 1, 2008 Share Posted May 1, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/103614-why-declare-new/#findComment-530882 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.