Javascript
/
Objects
- 1 Fundamentals 11
-
Hello World S
-
Code structure S
-
Use strict S
-
Variables S
-
Data types S
-
Type conversions S
-
Maths S
-
Comparitions S
-
Conditional S
-
Loops S
-
Functions S
- 2 Testing 2
-
Mocha S
-
Nested describe S
- 3 Objects 4
-
Basics S
-
Reference S
-
Methods S
-
Constructor S
- 4 Types 5
-
Primitives S
-
Numbers S
-
Strings S
-
Arrays S
-
Json S
- 5 Classes 3
-
Constructor S
-
Binding S
-
Inheritance S
S
R
Q
Javascript Objects Methods
Actions are functions in properties Arrow functions have no this let B = { age: 20, sayHi() { let a = () => this.age }Arrow
Methods
A function that is the property of an object is called method.
/**
* In JavaScript actions are represented by
* functions in properties
*
* We can use a shorthand syntax () {}
*/
let A = {
name: "John",
age: 30,
hello() { // same as "sayHi: function()"
console.log("Hello");
}
};
A.world = function() {
console.log("World");
};
A.hello(); // Hello
A.world(); // World
This
Sometimes object method needs to access object properties.
/**
* To access the object itself, a method uses
* this keyword
*
* Arrow functions have no this,
* it is taken from the outer normal function
*/
let A = {
name: "John",
sayHi() {
console.log(
this.name // Look Here
);
}
};
let B = {
name: "Ilya",
sayHi() {
let c = () => console.log(this.name);
c();
}
};
A.sayHi(); // John
B.sayHi(); // Ilya
➥ Questions
Last update: 117 days ago