Cover by Cody Lindley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

O'Reilly logo

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required