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

Commit782f500

Browse files
committed
Added __proto__ inheritence in js
1 parent150e32c commit782f500

File tree

5 files changed

+116
-23
lines changed

5 files changed

+116
-23
lines changed

‎00 datatypes/1. equality/index.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<htmllang="en">
3+
<head>
4+
<metacharset="UTF-8"/>
5+
<metahttp-equiv="X-UA-Compatible"content="IE=edge"/>
6+
<metaname="viewport"content="width=device-width, initial-scale=1.0"/>
7+
<title>Equality and sameness</title>
8+
<scriptsrc="index.js"></script>
9+
</head>
10+
<body>
11+
<h1>Equality and Sameness</h1>
12+
</body>
13+
</html>

‎00 datatypes/1. equality/index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// console.log(0 == '') // true
2+
// console.log(0 == '\n') // true
3+
// console.log(0 == '\t') // true
4+
// console.log(0 == ' ') // true
5+
// console.log(0 == '\r') // true
6+
// console.log(0 == '\s') // false
7+
// console.log(0 == '\f') // true
8+
// console.log(Number('\n')) // 0
9+
10+
// console.log(Number(true)) // 1
11+
// console.log(Number(false)) // 0
12+
13+
14+
// let A == B and If datatype of a and b is same then type casting won't happen.
15+
16+
// console.log(0 == false)
17+
// console.log('' == false)
18+
// console.log(' ' == 'false') // means ' ' === 'false' => false
19+
// console.log(0 == '') // true bcoz Number('') => 0
20+
// console.log(0 == " ") //true
21+
// console.log('false' == false) // false
22+
23+
console.log([1,2]==[1,2])// false, bcoz of different references in memory
24+
leta=[1,2]
25+
letb=a;
26+
console.log(b==a)// true, same references in memory
Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// This keyword -> refers to the JS context object in which the current
2-
// code is running
2+
// code is running or which object has called the function
33

44
// console.log(this); // window object
55

@@ -10,48 +10,50 @@
1010

1111
// f();
1212

13-
// var f = function(){
14-
// console.log(this); // window object
15-
// }
1613

17-
// f();
18-
19-
// var f = ()=>{
14+
// var f = function(){
2015
// console.log(this); // window object
2116
// }
2217

2318
// f();
24-
19+
// console.log(this)
2520
// var obj = {
26-
// name:this,
27-
// print:function(){
21+
// name: "rajan",
22+
// print:function (){
2823
// console.log(this); // obj = {name: Window, print: ƒ}
2924
// }
3025
// }
3126

3227
// obj.print();
3328

3429
// var obj = {
35-
// name:"rajan",
30+
// name: this,
3631
// print: ()=>{
32+
// console.log(name )
3733
// console.log(this); // window object
3834
// }
3935
// }
4036

4137
// obj.print();
4238

43-
// var obj = {
44-
// name:"rajan",
45-
// student:{
46-
// print: ()=>{
47-
// console.log(this); // window object
48-
// },
49-
// print2: function(){
50-
// console.log(this);// student object
51-
// }
52-
// }
53-
54-
// }
39+
varobj={
40+
name:"rajan",
41+
student:{
42+
age:22,
43+
print:()=>{
44+
console.log(this);// window object
45+
},
46+
print2:function(){
47+
console.log(this);// student object
48+
}
49+
}
50+
51+
}
5552

5653
// obj.student.print();
5754
// obj.student.print2();
55+
56+
57+
letz=obj.student.print2
58+
console.log(typeofz)
59+
z()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<htmllang="en">
3+
<head>
4+
<metacharset="UTF-8"/>
5+
<metahttp-equiv="X-UA-Compatible"content="IE=edge"/>
6+
<metaname="viewport"content="width=device-width, initial-scale=1.0"/>
7+
<title>Object __proto__ inheritence</title>
8+
<scriptsrc="script.js"></script>
9+
</head>
10+
<body></body>
11+
</html>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// __proto__ should be used for debugging purpose as JS says
2+
// In JS inheritence works using __proto__ in objects
3+
4+
letstu1={
5+
name:"Rajan",
6+
age:22
7+
}
8+
9+
letstu2=Object.create(stu1)
10+
// console.log(stu1) // { name: 'Rajan', age: 22 }
11+
// console.log(stu2) // {}
12+
13+
// but following is coming form stu1 object
14+
15+
// console.log(stu2.name) // this is comming form stu1
16+
// console.log(stu2.age) // coming form stu1 object
17+
// console.log(stu2.__proto__) // stu1 obj
18+
// console.log(stu2.__proto__ == stu1)
19+
// console.log(stu1.__proto__) // Object
20+
// console.log(stu1.__proto__._proto__) // null
21+
22+
// inheritence chain is stu2 -> stu1 -> Object -> null
23+
// stu2.class = "MCA";
24+
// console.log(stu2.class) // MCA
25+
// console.log(stu2) // { class: 'MCA' }
26+
27+
// // Shadowing the property happens in write operation
28+
29+
// stu2.name = "Mukul"; // created name in stu2 obj
30+
// console.log(stu2) // { class: 'MCA', name: 'Mukul' }
31+
32+
stu1.age++;
33+
console.log(stu1.age)// 23
34+
console.log(stu2.age)//23
35+
36+
stu2.age++;// means stu2.age(write means create age in stu2 obj) = stu2.age(here reading is happening from stu1.age) + 1
37+
console.log(stu2.age)// 24
38+
console.log(stu1)// { name: 'Rajan', age: 23 }
39+
console.log(stu2)// { age: 24 }
40+
41+

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp