Skip to content

Instantly share code, notes, and snippets.

@jayjariwala
Last active January 19, 2018 21:02
Show Gist options
  • Save jayjariwala/f7783766a0c3e3859390732afc107148 to your computer and use it in GitHub Desktop.
Save jayjariwala/f7783766a0c3e3859390732afc107148 to your computer and use it in GitHub Desktop.
YDKJS - Objects

Objects

Syntax

Object come in two forms

  1. the declarative form (litral)
  2. Constrcted form

Declarative/litral Object

var myObj = {
  key:value
 // ...
 }

Constrcted form

 var myObj = new Object();
 myObj.key = value;

Example

var strPrimitive = "I am a string";
console.log( strPrimitive.length ); // 13
console.log( strPrimitive.charAt( 3 ) ); // "m"

In both cases, we call a property or method on a string primitive, and the engine automatically coerces it to a String object, so that the property/method access works.

Build-in Objects There are several other object subtypes, usually referred to as built-in objects.

  • String
  • Number
  • Boolean
  • Object
  • Function
  • Array
  • Date
  • RegExp
  • Error

To access the value at the location a in myObject, we need to use either the . operator or the [ ] operator. The .a syntax is usually referred to as “property access,” whereas the ["a"] syntax is usually referred to as “key access.” In reality, they both access the same location and will pull out the same value, 2, so the terms can be used interchangeably. We will use the most common term, “property access,” from here on.

Special Values

Null is an empty value undegined is a missing value

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment