Skip to content

Commit a5527dd

Browse files
committed
encapsulation chapter: javascript => DONE
1 parent 181035a commit a5527dd

File tree

5 files changed

+90
-52
lines changed

5 files changed

+90
-52
lines changed

book/encapsulation.tex

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,26 @@ \subsection{Java}
6363
\subsection{Javascript}
6464

6565
All fields with the keyword \keyword{this} are public.
66-
All methods defined using the same keyword inside the constructor or using the prototype are public.
66+
All methods defined using the same keyword inside the constructor or using the prototype are public.
6767

68-
%\lstinputlisting[linerange={1-1,5-5}, style=codeStyle]{../codes/javascript/person2.js}
69-
%
70-
%\lstinputlisting[linerange={1-1,15-15}, style=codeStyle]{../codes/javascript/person.js}
68+
\lstinputlisting[linerange={3-4,12-12,16-16,22-22}, style=codeStyle]{../codes/javascript/encapsulation/person.js}
69+
70+
Which can be accessed anywhere
71+
72+
\lstinputlisting[linerange={1-1,4-4,7-7,12-12}, style=codeStyle]{../codes/javascript/encapsulation/app.js}
7173

74+
Actually, objects in Javascript can be updated by assigning them new fields and methods dynamically.
75+
This gives you the power to upgrade existing prototypes (classes) and decorate objects without the need to create more classes.
76+
Also, as \nameword{uncle Ben} of \nameword{Spider-man} once said: ``\textit{With great power comes great responsibility}".
77+
78+
\lstinputlisting[linerange={1-17}, style=codeStyle]{../codes/javascript/encapsulation/decorate.js}
79+
80+
Here, we created two objects, but with different fields.
81+
Then, we can create a function which print different fields of a Person.
82+
Just a note: you have to verify that the member is not a function, since functions in Javascript are first class objects.
83+
Which means: they are a type (\keyword{function}), they are instances of \keyword{Object} and they can be treated as any variable.
84+
85+
\lstinputlisting[linerange={19-29}, style=codeStyle]{../codes/javascript/encapsulation/decorate.js}
7286

7387
\subsection{Lua}
7488

@@ -156,13 +170,15 @@ \subsection{Java}
156170

157171
\lstinputlisting[language=Java, linerange={3-3,7-7,29-29,31-34}, style=codeStyle]{../codes/java/src/encapsulation/core/Person.java}
158172

159-
160173
\subsection{Javascript}
161174

162-
%\lstinputlisting[linerange={1-1,5-5}, style=codeStyle]{../codes/javascript/person2.js}
163-
164-
%\lstinputlisting[linerange={1-1,15-15}, style=codeStyle]{../codes/javascript/person.js}
175+
There is no protected members in Javascript.
176+
You can find some hacks on the web, but they can be expensive in term of memory and processing power.
177+
To emulate such mechanism, you have to know the caller object
178+
If you insist on having protected and private members, some libraries can be helpful such as \nameword{mozart}\footnote{mozart: \url{https://github.com/philipwalton/mozart}}.
165179

180+
% https://philipwalton.com/articles/implementing-private-and-protected-members-in-javascript/
181+
% https://philipwalton.com/articles/implementing-private-and-protected-members-in-javascript/
166182

167183
\subsection{Lua}
168184

@@ -269,9 +285,13 @@ \subsection{Java}
269285

270286
\subsection{Javascript}
271287

272-
%\lstinputlisting[linerange={1-1,5-5}, style=codeStyle]{../codes/javascript/person2.js}
273-
%
274-
%\lstinputlisting[linerange={1-1,15-15}, style=codeStyle]{../codes/javascript/person.js}
288+
There is a convention to use underscore \keyword{\_} as a mean to signal a class member as private.
289+
But the member still accessible as public.
290+
Another way is to use closures (the combination of a function and the lexical environment within which that function was declared), by defining a field inside the constructor using the keyword \keyword{var} or \keyword{let}.
291+
But, any method using this variable must be defined inside the constructor.
292+
This means each time you create a new instance (object), a new function will be created for this object.
293+
294+
275295

276296

277297
\subsection{Lua}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var Person = require("person.js");
2+
var Student = require("student.js");
3+
4+
var p = new Person("Karim_p");
5+
var s = new Student("Karim_s");
6+
7+
console.log(p.luckyNumber);
8+
console.log(p._name);
9+
console.log(p.num);//undefined
10+
console.log(p.t);
11+
12+
p.info();
13+
s.info();
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
let Person = function(name) {
2+
this.name = name;
3+
}
4+
5+
function create(name, members) {
6+
let p = new Person(name);
7+
p = Object.assign(p, members);
8+
return p;
9+
}
10+
11+
let student = create ("Karim_s", {
12+
year: 2
13+
});
14+
15+
let teacher = create ("Karim_t", {
16+
courses: ["C1", "C2"]
17+
});
18+
19+
function info (person) {
20+
console.log(person.name);
21+
for(let member in person){
22+
if (typeof member != "function") {
23+
console.log(member, " = ", person[member]);
24+
}
25+
}
26+
}
27+
28+
info(student);
29+
info(teacher);
Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,22 @@
1+
module.exports = Person;
2+
13
function Person(name) {
24
this.luckyNumber = 0;
35
this._name = name;
46
var num = Person.nbr;
57
Person.nbr++;
68

79
this.info1 = function(){
8-
console.log("My name: ${this._name}");
910
console.log("My number is: ${num}");
1011
}
1112
}
1213

1314
Person.nbr = 0;
1415

1516
Person.prototype.info = function(){
16-
console.log("My name: " + this.name + ", My birth year: " + this.byear);
17-
}
18-
19-
Person.population = function(){
20-
return Person.nbr;
17+
console.log("My name: ${this._name}");
18+
this.info1();
19+
console.log("I am a: " + t);
20+
console.log("My lucky number is: " + luckyNumber);
21+
console.log("--------------------------");
2122
}
22-
23-
var p = new Person("Karim", 1986);
24-
var p2 = new Person("Karim+1", 1987);
25-
26-
p.info();
27-
p2.info();
28-
var nbr = Person.population();
29-
30-
console.log("The number of persons: " + nbr);
31-
p2 = null;
32-
nbr = Person.population();
33-
console.log("The number of persons: " + nbr);
Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,14 @@
1-
function Person(name, byear) {
2-
this.name = name;
3-
this.byear = byear;
4-
Person.nbr++;
5-
}
6-
7-
Person.nbr = 0;
1+
var Person = require("person.js");
82

9-
Person.prototype.info = function(){
10-
console.log("My name: " + this.name + ", My birth year: " + this.byear);
11-
}
3+
module.exports = Student;
124

13-
Person.population = function(){
14-
return Person.nbr;
5+
function Student(name) {
6+
Person.call(this, name);
7+
this._name = name + "2";//can be accessed
8+
console.log("Student => num = ", num, ", this.num = ", this.num);
9+
this.t = "student";
10+
luckyNumber = 1;
1511
}
1612

17-
var p = new Person("Karim", 1986);
18-
var p2 = new Person("Karim+1", 1987);
19-
20-
p.info();
21-
p2.info();
22-
var nbr = Person.population();
23-
24-
console.log("The number of persons: " + nbr);
25-
p2 = null;
26-
nbr = Person.population();
27-
console.log("The number of persons: " + nbr);
13+
Student.prototype = Object.create(Person.prototype);
14+
Student.constructor = Student;

0 commit comments

Comments
 (0)