Skip to content

Instantly share code, notes, and snippets.

@anthonybrown
Created May 26, 2017 00:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anthonybrown/1faa0cc1176566e279b160530622674e to your computer and use it in GitHub Desktop.
Save anthonybrown/1faa0cc1176566e279b160530622674e to your computer and use it in GitHub Desktop.
New and Window Binding // source https://jsbin.com/vegusa
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<meta name='description' content='JavaScript New &amp; Window Binding'>
<title>New and Window Binding</title>
</head>
<body>
<div id='msg'></div>
<script id="jsbin-javascript">
/*
- Implicate Binding
- Explicit Binding
- new Binding
- window Binding
*/
document.addEventListener("DOMContentLoaded", function(e) {
// New Binding
var msg = document.getElementById('msg');
var Animal = function (color, name, type) {
// this = {}
this.color = color;
this.name = name;
this.type = type;
};
Animal.prototype.greet = function () {
console.log(`My name is ${this.name} and I am a ${this.color} ${this.type}!`);
return msg.innerHTML += `My name is ${this.name} and I am a ${this.color} ${this.type}`;
};
var zebra = new Animal('black and white', 'Zorro', 'Zebra');
msg.innerHTML += zebra.greet();
// Window Binding
// var sayAge = function() {
/* using strict mode */
/* throws an immediate error.*/
// 'use strict';
// console.log(this.age);
// return msg.innerHTML += '<br>'+this.age + '<br>';
// };
// var me = {
// age: 33
// };
// sayAge();
// window.age = 50;
// sayAge();
});
</script>
<script id="jsbin-source-javascript" type="text/javascript">/*
- Implicate Binding
- Explicit Binding
- new Binding
- window Binding
*/
document.addEventListener("DOMContentLoaded", function(e) {
// New Binding
var msg = document.getElementById('msg');
var Animal = function (color, name, type) {
// this = {}
this.color = color;
this.name = name;
this.type = type;
};
Animal.prototype.greet = function () {
console.log(`My name is ${this.name} and I am a ${this.color} ${this.type}!`);
return msg.innerHTML += `My name is ${this.name} and I am a ${this.color} ${this.type}`;
};
var zebra = new Animal('black and white', 'Zorro', 'Zebra');
msg.innerHTML += zebra.greet();
// Window Binding
// var sayAge = function() {
/* using strict mode */
/* throws an immediate error.*/
// 'use strict';
// console.log(this.age);
// return msg.innerHTML += '<br>'+this.age + '<br>';
// };
// var me = {
// age: 33
// };
// sayAge();
// window.age = 50;
// sayAge();
});
</script></body>
</html>
/*
- Implicate Binding
- Explicit Binding
- new Binding
- window Binding
*/
document.addEventListener("DOMContentLoaded", function(e) {
// New Binding
var msg = document.getElementById('msg');
var Animal = function (color, name, type) {
// this = {}
this.color = color;
this.name = name;
this.type = type;
};
Animal.prototype.greet = function () {
console.log(`My name is ${this.name} and I am a ${this.color} ${this.type}!`);
return msg.innerHTML += `My name is ${this.name} and I am a ${this.color} ${this.type}`;
};
var zebra = new Animal('black and white', 'Zorro', 'Zebra');
msg.innerHTML += zebra.greet();
// Window Binding
// var sayAge = function() {
/* using strict mode */
/* throws an immediate error.*/
// 'use strict';
// console.log(this.age);
// return msg.innerHTML += '<br>'+this.age + '<br>';
// };
// var me = {
// age: 33
// };
// sayAge();
// window.age = 50;
// sayAge();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment