Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tranduclinh2067/68aef27c5f27a46fa73ddf3256a108da to your computer and use it in GitHub Desktop.
Save tranduclinh2067/68aef27c5f27a46fa73ddf3256a108da to your computer and use it in GitHub Desktop.
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);
@tranduclinh2067
Copy link
Author

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());

@tranduclinh2067
Copy link
Author

Đị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