17.11.2015 Views

JavaScript_Succinctly

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Object() parameters<br />

The Object() constructor function takes one optional parameter. That parameter is the<br />

value you would like to create. If you provide no parameter, then a null or undefined<br />

value will be assumed.<br />

Sample: sample70.html<br />

<br />

// Create an empty object with no properties.<br />

var cody1 = new Object();<br />

var cody2 = new Object(undefined);<br />

var cody3 = new Object(null);<br />

console.log(typeof cody1, typeof cody2, typeof cody3); // Logs 'object<br />

object object'.<br />

<br />

If a value besides null or undefined is passed to the Object constructor, the value<br />

passed will be created as an object. So theoretically, we can use the Object()<br />

constructor to create any of the other native objects that have a constructor. In the next<br />

example, I do just that.<br />

Sample: sample71.html<br />

<br />

/* Use the Object() constructor to create string, number, array,<br />

function, Boolean, and regex objects. */<br />

// The following logs confirm object creation.<br />

console.log(new Object('foo'));<br />

console.log(new Object(1));<br />

console.log(new Object([]));<br />

console.log(new Object(function () { }));<br />

console.log(new Object(true));<br />

console.log(new Object(/\bt[a-z]+\b/));<br />

/* Creating string, number, array, function, Boolean, and regex object<br />

instances via the Object() constructor is really never done. I am just<br />

demonstrating that it can be done. */<br />

<br />

84

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!