Last active
January 11, 2018 08:52
-
-
Save husain100b/aebd2a5e6a309927a34180dd9a922a13 to your computer and use it in GitHub Desktop.
This in JavaScript
This file contains 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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Document</title> | |
</head> | |
<body> | |
<script> | |
/* | |
* Implicit Binding | |
*/ | |
// var me = { | |
// name: 'Husain', | |
// age: 29, | |
// sayName: function() { | |
// console.log(this.name); | |
// } | |
// } | |
// me.sayName(); | |
// var sayNameMixin = function (obj) { | |
// obj.sayName = function() { | |
// console.log(this.name, this.age); | |
// }; | |
// }; | |
// var me = { | |
// name: 'Husain', | |
// age: 29 | |
// }; | |
// var you = { | |
// name: 'Sami', | |
// age: 12 | |
// }; | |
// sayNameMixin(me); | |
// sayNameMixin(you); | |
// me.sayName(); | |
// you.sayName(); | |
// var Person = function(name, age) { | |
// return { | |
// name: name, | |
// age: age, | |
// sayName: function() { | |
// console.log(this.name); | |
// }, | |
// mother: { | |
// name: 'Nargis', | |
// sayName: function() { | |
// console.log(this.name); | |
// } | |
// } | |
// }; | |
// }; | |
// var jim = Person('Husain', 29); | |
// jim.sayName(); | |
// jim.mother.sayName(); | |
// ======================================== | |
// Explicit Binding | |
// var sayName = function(lang1, lang2, lang3) { | |
// console.log('My name is ' + this.name + ' and age is ' + this.age + ' and I know ' + lang1 + ', ' + lang2 +', ' + lang3); | |
// }; | |
// var stacey = { | |
// name: 'Nargis', | |
// age: 45, | |
// }; | |
// var languages = ['JavaScript', 'PHP', 'Python']; | |
// var newFn = sayName.bind(stacey, languages[0], languages[1], languages[2]); | |
// sayName.call(stacey, languages[0], languages[1], languages[2]); | |
// sayName.apply(stacey, languages); | |
// console.log('Here'); | |
// newFn(); | |
// ================== | |
// New Binding | |
// var Animal = function(color, name, type) { | |
// this.color = color; | |
// this.name = name; | |
// this.type = type; | |
// }; | |
// var zebra = new Animal('black and white', 'Zerro', 'Zebra'); | |
// =================== | |
// window binding | |
// var sayAge = function() { | |
// 'use strict'; | |
// console.log(this.age); | |
// }; | |
// var me = { | |
// age: 29, | |
// }; | |
// sayAge(); | |
// window.age = 29; | |
// sayAge(); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment