Skip to content

Instantly share code, notes, and snippets.

Created August 17, 2016 07:18
Show Gist options
  • Save anonymous/e057da596af5559e5cb7645f514f8bff to your computer and use it in GitHub Desktop.
Save anonymous/e057da596af5559e5cb7645f514f8bff to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/xeyeboxiqa
<!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>
// ------------------------------------------------------ //
// (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