-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConstructor_Prototype.js
40 lines (27 loc) · 1.14 KB
/
Constructor_Prototype.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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//Prototype property on function is NOT the prototype of the function.
//Rather, it is the prototype of the object created if new constructor used.
function Person(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
//This is the prototype of the objects created by the Person constructor.
//NOT the function's prototype
Person.prototype.getFullName = function() {
return this.firstName + " " + this.lastName;
}
var john = new Person ('John', 'Doe');
console.log(john);
//Built-In Function Constructors
//BETTER TO USE THE ACTUAL PRIMITIVE VALUES, instead of using constructors for primitives
var a = new Number(3);
var a = new String("Doe");
//a points to the .prototype property below for methods such as indexOf
String.prototype
"John" //Would be equal to calling new String("John");
//JS will not convert number to Number object automatically
3.isPositive() //Would not work
var a = new Number(3)
a.isPositive() //Would work
//DO NOT USE FOR IN LOOP FOR ARRAYS. Use normal for loops instead.
//Some frameworks/libraries may add functionalities to Array.prototype which would
//affect the outcome of FOR IN loops.