object Part 1
JS Object¶
Features:¶
- reference type.
- Change property after object has been created. eg:
- refer Class property inside function using
this
. else it wont work. - Side note : Each Objeect we create by any mean will have a
prototype
. default prototype isObject.prototype
which is root prototype of all objects.
A. Object basics¶
- accessing its property
1.1. using .
1.2. accessing its propertyusing ['abc']
//abc is property.
-
add another object as property.
-
Add Function in object. Notice
:
instead of=
. -
using string as property name. by this we can use
-
as well in var name. if use it directly it will give error. -
Create object using
new object()
- this is not preferred. -
Create object using
prototype
- Object.create(prototype) -
camparing objects using
==
it checks references.
B. PROTOTYPE¶
- Blueprints from where we can create multiple object and benefits its
functions
andfeilds
Object.prototype
(itself a object) is parent prtotype. try to priny itlog(Object.prototype ) //undefined- var1.anything() --> error; var1.toString() --> no error. how ?
- prototype can be think of as parent Object and can be used inside objects(created from prototype).
- Object.prototype is parent all object and all its method accessible to object derived from it.
- eg : to understand it more better:
-
Protyotype Chain:
-
var chilhObj = Object.create(parentObj)
-
Create multiple object from same blueprint
var chilhObj1 = Object.create(parentObj); var chilhObj2= Object.create(parentObj);
-
Get prototype:
var a = Object.create(x); Object.getPrototypeOf(a) //x
C. Constructor Functions¶
- Another way to create Object.
- eg:
function ABC(){ ... } var o1 = new ABC(); //protype of o1 will be ABC.protype. ABC.prototype.greet = function(){ log("hello");} o1.greet() // hello. var a =
- constrtor function with argument.
this
keyword¶
its little trick in JS, but we can handle it.
- binding
Using
function f1(){ console.log(this)} case1: invoke globally f1() // it will print window object. here this is bind with window object. case 2: invoke inside object var obj = { x : f1 ; } obj.x(); // it will print obj. here this is bind with obj.
bind()
function we can bind any object withthis
offunction f1(){ console.log(this)}
.
eg:
var obj = { x : f1 ; }
obj.x.bind(this); // here this is bind with obj again.
note: parenthesis is omitted after x.
var obj2 = { ... }
obj.x.bind(obj2); // here this is bind with obj2 but wont we called.
obj.x.bind(obj2)();//will be called here. Added parenthesis at end.
-
passing arg while binding
function f1(arg1,...){ console.log(arg1 + this)} var obj = { x : f1 ; } obj.x.bind(this,"arg1", ...)(); // 2nd arg onwards.
-
Call() method
- it binds and call.
-
no need to put addiotional parenthesis at end.
-
apply() method
- same as call but it accepts argument in array.
defineProperty¶
Object.defineProperty(...)
--> Add new property in object.
way1:
way2:
- its complex, but it provides some more configuration.
- 3rd argument is object --> {value: " " or f1(){}, writable: T/F, ...}
- by default value is readable.
- make it writable to update it.
-
other properties which 3rd argument (object) accepts other than
value
,writable
: -
get
: below below will be called whenever 'name' is accessed.
set
: below below will be called whenever 'name' is accessed.
google other ...
clear some property of object¶
- assign null
delete
:
check if property is present inside object¶
Note : Name - false, name - true
Iterate through all feilds:¶
feild name:
feild values: