Skip to content

Instantly share code, notes, and snippets.

@husain100b
Last active January 11, 2018 08:52
Show Gist options
  • Save husain100b/aebd2a5e6a309927a34180dd9a922a13 to your computer and use it in GitHub Desktop.
Save husain100b/aebd2a5e6a309927a34180dd9a922a13 to your computer and use it in GitHub Desktop.
This in JavaScript
<!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