Skip to content

Commit 560e2f6

Browse files
committed
encapsulation chapter: javascript => DONE (private)
1 parent a5527dd commit 560e2f6

File tree

4 files changed

+12
-11
lines changed

4 files changed

+12
-11
lines changed

book/encapsulation.tex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,12 @@ \subsection{Javascript}
291291
But, any method using this variable must be defined inside the constructor.
292292
This means each time you create a new instance (object), a new function will be created for this object.
293293

294+
\lstinputlisting[linerange={1-3,5-14}, style=codeStyle]{../codes/javascript/encapsulation/person.js}
294295

296+
The private member defined by convention can be accessed anywhere.
297+
In contrary, the member defined with closures cannot be accessed.
295298

299+
\lstinputlisting[linerange={1-1,4-4,8-9}, style=codeStyle]{../codes/javascript/encapsulation/app.js}
296300

297301
\subsection{Lua}
298302

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
var Person = require("person.js");
2-
var Student = require("student.js");
1+
var Person = require("./person.js");
2+
var Student = require("./student.js");
33

44
var p = new Person("Karim_p");
55
var s = new Student("Karim_s");
66

77
console.log(p.luckyNumber);
88
console.log(p._name);
99
console.log(p.num);//undefined
10-
console.log(p.t);
11-
10+
console.log("=========================");
1211
p.info();
1312
s.info();

codes/javascript/encapsulation/person.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@ function Person(name) {
77
Person.nbr++;
88

99
this.info1 = function(){
10-
console.log("My number is: ${num}");
10+
console.log("My number is: ", num);
1111
}
1212
}
1313

1414
Person.nbr = 0;
1515

1616
Person.prototype.info = function(){
17-
console.log("My name: ${this._name}");
17+
console.log("My name: ", this._name);
1818
this.info1();
19-
console.log("I am a: " + t);
20-
console.log("My lucky number is: " + luckyNumber);
19+
console.log("My lucky number is: ", luckyNumber);
2120
console.log("--------------------------");
2221
}

codes/javascript/encapsulation/student.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
var Person = require("person.js");
1+
var Person = require("./person.js");
22

33
module.exports = Student;
44

55
function Student(name) {
66
Person.call(this, name);
77
this._name = name + "2";//can be accessed
8-
console.log("Student => num = ", num, ", this.num = ", this.num);
9-
this.t = "student";
8+
//console.log("Student => num = ", num, ", this.num = ", this.num); //error
109
luckyNumber = 1;
1110
}
1211

0 commit comments

Comments
 (0)