Created
November 1, 2018 16:02
-
-
Save tranduclinh2067/68aef27c5f27a46fa73ddf3256a108da to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Obj = () => { | |
var id =1; | |
return { | |
getID: () => { | |
return id; | |
}, | |
setID: (id2) => { | |
id=id2; | |
// console.log(id2); //* | |
}}} | |
var In = Obj(); | |
console.log(In.getID()); | |
In.setID(100); | |
console.log(In.getID()); | |
//In.setID(100); |
Định nghĩa đối tượng thì: Closures nhanh hơn.
Khả năng tiết kiệm bộ nhớ và khởi tạo đối tượng mới: Prototype nhanh hơn.
Truy cập hàm (getID, setID): Prototype nhanh hơn.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cách viết Closures nhược điểm khi chạy code lâu hơn Prototype nhiều.
Ví du:
function Obj () {
this.id = 1;
}
Obj.prototype.getID = function(){
return this.id;
};
Obj.prototype.setID = function(id2){
this.id = id2;
};
var In = new Obj();
console.log(In.getID()); // => 1
In.setID(100);
console.log(In.getID());