Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd176a2b

Browse files
committed
Classes and functional approach
1 parentc9088c5 commitd176a2b

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
####The New Keyword
2+
3+
```js
4+
// The new keyword
5+
// The new keyword creates an object of num
6+
// which then let's us access to various methods,
7+
// like .toFixed() in the below code.
8+
constnum=newNumber(100.25);
9+
10+
console.log(num.toFixed(0));// 100
11+
```
12+
13+
####This Keyword
14+
15+
```js
16+
constperson= {
17+
name:"John",
18+
getName() {
19+
console.log(this);
20+
},
21+
};
22+
23+
person.getName();// { name: 'John', getName: [Function: getName] }
24+
```
25+
26+
####Class
27+
28+
```js
29+
// Class is a schema for
30+
// an object that can save
31+
// many values
32+
33+
classPerson {
34+
constructor(name,age,isWorking) {
35+
this.name= name;
36+
this.age= age;
37+
this.isWorking= isWorking;
38+
}
39+
}
40+
41+
constuser=newPerson("John",22,true);
42+
43+
console.log(user);// Person { name: 'John', age: 22, isWorking: true }
44+
45+
// How can we do the same thing above using
46+
// arrow function ?
47+
48+
constcreatePerson= (name,age,isWorking)=> ({ name, age, isWorking });
49+
50+
constuser1=createPerson("Shubham",23,false);
51+
console.log(user1);//{ name: 'Shubham', age: 23, isWorking: false }
52+
```
53+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// The new keyword
2+
// The new keyword creates an object of num
3+
// which then let's us access to various methods,
4+
// like .toFixed() in the below code.
5+
// const num = new Number(100.25);
6+
7+
// console.log(num.toFixed(0)); // 100
8+
9+
// const person = {
10+
// name: "John",
11+
// getName() {
12+
// console.log(this);
13+
// },
14+
// };
15+
16+
// person.getName(); // { name: 'John', getName: [Function: getName] }
17+
18+
// Class is a schema for
19+
// an object that can save
20+
// many values
21+
22+
// class Person {
23+
// constructor(name, age, isWorking) {
24+
// this.name = name;
25+
// this.age = age;
26+
// this.isWorking = isWorking;
27+
// }
28+
// }
29+
30+
// const user = new Person("John", 22, true);
31+
32+
// console.log(user);
33+
34+
// HOw can we do the same thing above using
35+
// arrow function ?
36+
37+
constcreatePerson=(name,age,isWorking)=>({ name, age, isWorking});
38+
39+
constuser1=createPerson("Shubham",23,false);
40+
console.log(user1);//{ name: 'Shubham', age: 23, isWorking: false }

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp