Skip to content

Instantly share code, notes, and snippets.

View conanak99's full-sized avatar

Phạm Huy Hoàng conanak99

View GitHub Profile
var person = {
firstName: 'Hoang',
lastName: 'Pham',
showName: function() {
console.log(this.firstName + ' ' + this.lastName);
}
};
//Ở đây this sẽ là object person
person.showName(); //Hoang Pham.
var person = {
firstName: 'Hoang',
lastName: 'Pham',
showName: function() {
console.log(this.firstName + ' ' + this.lastName);
}
};
$('button').click(person.showName); //showName truyền vào như callback, ở đây this chính là button
var person = {
firstName: 'Hoang',
lastName: 'Pham',
friends : ['Minh', 'Sang', 'Khoa', 'Hoang'],
showFriend: function() {
for(var i = 0; i < this.friends.length; i++)
console.log(this.firstName + ' have a friend named ' + this.friends[i]);
},
showFriendThis: function() {
this.friends.forEach(function(fr){
var person = {
firstName: 'Hoang',
lastName: 'Pham',
friends : ['Minh', 'Sang', 'Khoa', 'Hoang'],
showFriendFixed: function() {
var personObj = this; //Gán giá trị this vào biến personObj
this.friends.forEach(function(fr){
console.log(personObj.firstName + ' have a friend named ' + fr);
});
}
var person = {
firstName: 'Hoang',
lastName: 'Pham',
showName: function() {
console.log(this.firstName + ' ' + this.lastName);
}
};
//Ở đây this sẽ là object person, chạy đúng
person.showName(); //Hoang Pham.
var person = {
firstName: 'Hoang',
lastName: 'Pham',
showName: function() {
console.log(this.firstName + ' ' + this.lastName);
}
};
var showNameFunc = person.showName.bind(person); //Gán function vào biến showNameFunc
showNameFunc(); //Chạy đúng vì this bây giờ là object person, do ta đã bind
@conanak99
conanak99 / 0_reuse_code.js
Created January 7, 2016 18:19
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
public string getUpperName(string name)
{
return name.ToUpper();
}
// Hàm A này sẽ bị lỗi
var upperNames = _db.Users.Select(x => getUpperName(x.Name)).ToList();
// Hàm B này không lỗi
var upperNames = _db.Users.Select(x => x.Name.ToUpper()).ToList();
public class Student {
public string Name { get; set;}
public int Age { get; set;}
// Format class này dưới dạng text, html, json để in ra
public string GetStudentInfoText() {
return "Name: " + Name + ". Age: " + Age;
}
public string GetStudentInfoHTML() {
return "<span>" + Name + " " + Age + "</span>";
request({
url: this._analyzeApiUrl, // Thay bằng URL của API
headers: {
"Ocp-Apim-Subscription-Key": this._analyzeKey // Thay bằng key của bạn
},
method: "POST",
json: true,
body: {
"url": imageUrl // Thay bằng url của hình ảnh
}