Created
August 17, 2016 07:18
-
-
Save anonymous/e057da596af5559e5cb7645f514f8bff to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/xeyeboxiqa
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> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
// ------------------------------------------------------ // | |
// (1) Object function with `prototype` | |
// ------------------------------------------------------ // | |
var Pokemon = function (pokemonName) { | |
this.name = pokemonName; | |
}; | |
Pokemon.prototype.bark = function () { | |
console.log("Hey, I'm " + this.name); // this here refers to "Pokemon" | |
}; | |
var pikachu = new Pokemon('Pikachu'); | |
pikachu.bark(); | |
// ------------------------------------------------------ // | |
// (2) Object function without `prototype` | |
// ------------------------------------------------------ // | |
var Pokemon = function(pokemonName) { | |
this.name = pokemonName; | |
this.bark = function() { | |
console.log("Hey, I'm " + this.name); | |
}; | |
}; | |
var pikachu = new Pokemon('Pikachu'); | |
pikachu.bark(); | |
// ------------------------------------------------------ // | |
// (3) Object Literal | |
// ------------------------------------------------------ // | |
var Pokemon = { | |
name: "Pikachu", | |
bark: function() { | |
console.log("Hey, I'm " + this.name); | |
} | |
}; | |
Pokemon.bark(); | |
// ------------------------------------------------------ // | |
// (4) IIF: Immediately Invoked Function | |
// ------------------------------------------------------ // | |
var Pokemon = (function(){ | |
var name; | |
var setName = function(newName) { | |
name = newName; | |
}; | |
var bark = function () { | |
console.log("Hey, I'm " + name); | |
}; | |
return { | |
bark: bark, | |
setName: setName | |
}; | |
})(); | |
Pokemon.bark() | |
Pokemon.setName('Pikachu') | |
Pokemon.bark() | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript"> | |
// ------------------------------------------------------ // | |
// (1) Object function with `prototype` | |
// ------------------------------------------------------ // | |
var Pokemon = function (pokemonName) { | |
this.name = pokemonName; | |
}; | |
Pokemon.prototype.bark = function () { | |
console.log("Hey, I'm " + this.name); // this here refers to "Pokemon" | |
}; | |
var pikachu = new Pokemon('Pikachu'); | |
pikachu.bark(); | |
// ------------------------------------------------------ // | |
// (2) Object function without `prototype` | |
// ------------------------------------------------------ // | |
var Pokemon = function(pokemonName) { | |
this.name = pokemonName; | |
this.bark = function() { | |
console.log("Hey, I'm " + this.name); | |
}; | |
}; | |
var pikachu = new Pokemon('Pikachu'); | |
pikachu.bark(); | |
// ------------------------------------------------------ // | |
// (3) Object Literal | |
// ------------------------------------------------------ // | |
var Pokemon = { | |
name: "Pikachu", | |
bark: function() { | |
console.log("Hey, I'm " + this.name); | |
} | |
}; | |
Pokemon.bark(); | |
// ------------------------------------------------------ // | |
// (4) IIF: Immediately Invoked Function | |
// ------------------------------------------------------ // | |
var Pokemon = (function(){ | |
var name; | |
var setName = function(newName) { | |
name = newName; | |
}; | |
var bark = function () { | |
console.log("Hey, I'm " + name); | |
}; | |
return { | |
bark: bark, | |
setName: setName | |
}; | |
})(); | |
Pokemon.bark() | |
Pokemon.setName('Pikachu') | |
Pokemon.bark() | |
</script></body> | |
</html> |
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
// ------------------------------------------------------ // | |
// (1) Object function with `prototype` | |
// ------------------------------------------------------ // | |
var Pokemon = function (pokemonName) { | |
this.name = pokemonName; | |
}; | |
Pokemon.prototype.bark = function () { | |
console.log("Hey, I'm " + this.name); // this here refers to "Pokemon" | |
}; | |
var pikachu = new Pokemon('Pikachu'); | |
pikachu.bark(); | |
// ------------------------------------------------------ // | |
// (2) Object function without `prototype` | |
// ------------------------------------------------------ // | |
var Pokemon = function(pokemonName) { | |
this.name = pokemonName; | |
this.bark = function() { | |
console.log("Hey, I'm " + this.name); | |
}; | |
}; | |
var pikachu = new Pokemon('Pikachu'); | |
pikachu.bark(); | |
// ------------------------------------------------------ // | |
// (3) Object Literal | |
// ------------------------------------------------------ // | |
var Pokemon = { | |
name: "Pikachu", | |
bark: function() { | |
console.log("Hey, I'm " + this.name); | |
} | |
}; | |
Pokemon.bark(); | |
// ------------------------------------------------------ // | |
// (4) IIF: Immediately Invoked Function | |
// ------------------------------------------------------ // | |
var Pokemon = (function(){ | |
var name; | |
var setName = function(newName) { | |
name = newName; | |
}; | |
var bark = function () { | |
console.log("Hey, I'm " + name); | |
}; | |
return { | |
bark: bark, | |
setName: setName | |
}; | |
})(); | |
Pokemon.bark() | |
Pokemon.setName('Pikachu') | |
Pokemon.bark() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment