Chapter 2. Working with Objects and Properties

Complex Objects Can Contain Most of the JavaScript Values as Properties

A complex object can hold any permitted JavaScript value. Below, I create an Object() object called myObject and then add properties representing the majority of values available in JavaScript.

Live Code

<!DOCTYPE html><html lang="en"><body><script>

var myObject = {};

/* contain properties inside of myObject representing most of the native 
JavaScript values */

myObject.myFunction = function() {};
myObject.myArray = [];
myObject.myString = 'string';
myObject.myNumber = 33;
myObject.myDate = new Date();
myObject.myRegExp = /a/;
myObject.myNull = null;
myObject.myUndefined = undefined;
myObject.myObject = {};
myObject.myMath_PI = Math.PI;
myObject.myError = new Error('Crap!');

console.log(myObject.myFunction,myObject.myArray,myObject.myString,
  myObject.myNumber,myObject.myDate,myObject.myRegExp,myObject.myNull,
  myObject.myNull,myObject.myUndefined,myObject.myObject,
  myObject.myMath_PI,myObject.myError);

/* works the same with any of the complex objects, for example a function */

var myFunction = function() {};

myFunction.myFunction = function() {};
myFunction.myArray = [];
myFunction.myString = 'string';
myFunction.myNumber = 33;
myFunction.myDate = new Date();
myFunction.myRegExp = /a/;
myFunction.myNull = null;
myFunction.myUndefined = undefined;
myFunction.myObject = {};
myFunction.myMath_PI = Math.PI;
myFunction.myError = new Error('Crap!'); console.log(myFunction.myFunction,myFunction.myArray,myFunction.myString, ...

Get JavaScript Enlightenment now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.