-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.js
25 lines (22 loc) · 846 Bytes
/
class.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// from JavaScript Patterns by Stefanov
// bad
var Person = function (name) {
this.name = name;
this.say = function () {
return "I am " + this.name;
};
};
// When you invoke the constructor function with new, the following happens inside the function:
// An empty object is created and referenced by this variable, inheriting the prototype of the function.
// Properties and methods are added to the object referenced by this.
// The newly created object referenced by this is returned at the end implicitly (if no other object was returned explicitly).
//
// the say() method was added to this.
// The result is that any time you call new Person() a new function is created in memory.
// good
var Person = function (name) {
this.name = name;
};
Person.prototype.say = function () {
return "I am " + this.name;
};