Please enable JavaScript.
Coggle requires JavaScript to display documents.
JS OBJECT, :wastebasket:, :star2:, :man-running::skin-tone-5:, :-1::skin…
JS OBJECT
Object
:car:
Properties
car.name = Fiat
car.model = 500
car.weight = 850kg
car.color = red
All cars have the same properties,
but the property values differ from car to car.
Methods
car.start()
car.drive()
car.brake()
car.stop()
All cars have the same methods,
but the methods are performed at different times.
in JS
All JavaScript values, except
primitives
, are objects. Booleans, Numbers, Strings can be objects (if defined with the
new
keyword)
A primitive value is a value that has no properties or methods
Do not declare
Strings
,
Numbers
,
and
Booleans
as Objects!
When a JavaScript variable is declared with the keyword "new", the variable is created as an object
:red_cross:
var x = new String();
// Declares x as a String object
var y = new Number();
// Declares y as a Number object
var z = new Boolean();
// Declares z as a Boolean object
Avoid
String
,
Number
, and
Boolean
objects. They complicate your code and slow down execution speed
Properties
The
name:values
pairs in JavaScript objects are called
properties
Accessing Properties
objectName.propertyName
john.lastName;
john.birthYear;
etc
objectName["propertyName"]
person['lastName'];
var x = 'birthYear';
console.log(john[x]);
Methods
A method is a function
stored as a property.
var person = {
firstName: "John",
lastName : "Doe",
id : 5566,
fullName : function() {
return this.firstName + " " + this.lastName;
}
};
Accessing Methods
objectName.methodName()
name = person.fullName();
If you access a method without the () parentheses, it will return the function definition
'
this
'
Keyword
In a function definition, this refers to the "owner" of the function.
var john = {
firstName: 'John',
birthYear: 1992,
calcAge: function () {
return 2018 - this.birthYear;
}
};
call it:
console.log(john.calcAge());
var john = {
firstName: 'John',
birthYear: 1992,
calcAge: function() {
this.age = this.birthYear;
}
};
call it:
john.calcAge();
console.log(john);
As
variables
can contain
single values
var car = "Fiat";
many values
OBJECT !!!
var john= {
firstName: "John",
lastName:"Smith",
birthYear: 1990,
family: ['Jane', 'Mark', 'Bob', 'Emily'],
job: 'teacher',
isMarried: false
};
Object
vs Array
Array
The order matters a lot
Object
It does not matter at all
:wastebasket:
:star2:
:man-running::skin-tone-5:
:-1::skin-tone-4:
:speaking_head_in_silhouette:
:bow_and_arrow: