object Part 2
Object¶
A. Object Creation¶
-
first way: {}.
-
Could define
another object
as property of object. -
Could define
method
as property of object. -
always use this keyword to access object property, otherwise it would look for property in global execution stack's declaration, VO object.
- define property name as string.
- Second way: new Object()
- Object() is
constructor Function
- another eg:
- Third way: from
prototype
- All js object has one property called prototype . anything will be inherit down which is referenced by this property.
-
eg: add new function in prototype and access it.
-
Prototype
are JS way of inhertance, inherit property and method from parent object. Object.create( prototype1 : Object)
-
eg1: object.create(null);
-
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:
-
fourth way: custom Contructor function
-
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: