Skip to content

object Part 2

Object

A. Object Creation

  1. first way: {}.

  2. Could define another object as property of object.

  3. Could define method as property of object.

  4. always use this keyword to access object property, otherwise it would look for property in global execution stack's declaration, VO object.

  5. define property name as string.

  1. Second way: new Object()
  2. Object() is constructor Function
  3. another eg:

  1. Third way: from prototype
  2. All js object has one property called prototype . anything will be inherit down which is referenced by this property.
  3. eg: add new function in prototype and access it.

  4. Prototype are JS way of inhertance, inherit property and method from parent object.

  5. Object.create( prototype1 : Object)
  6. eg1: object.create(null);

  7. eg2:

More about Prototype :

  • Object is parent protype of all JS object. eg: toString() is deined here thats why accessible from any JS object.

  • prototype chain:


  1. fourth way: custom Contructor function

  2. just like noramla function and used with new operator.


Summary : Object Creation

  • internally they all are same.

THIS keyword

  • it refers to current object.
  • understand by below eg:

  • in JS developer could change binding behaviour, using bind()

  • eg: again bind it window object.

  • Can bind with nay object. eg: