Skip to content

Instantly share code, notes, and snippets.

@endeepak
Last active December 14, 2015 08:09
Show Gist options
  • Select an option

  • Save endeepak/5055790 to your computer and use it in GitHub Desktop.

Select an option

Save endeepak/5055790 to your computer and use it in GitHub Desktop.
// Basics
"foo" // string
'foo' // string
var foo = {} // new object
[1, 2, "foo", "bar"] // new array
var foo = {
name: 'Foo',
Greet: function(otherName){
console.log("Hello I am " + this);
console.log("Hello " + otherName + " my name is " + this.name);
}
}
/// Bindings
var Person = function(name){
this.Name = name;
this.Greet = function(otherName){
console.log("Hello I am " + this);
console.log("Hello " + otherName + " my name is " + this.Name);
}
}
var foo = new Person('foo');
var bar = new Person('bar');
foo.Greet('John Doe');
bar.Greet('John Doe');
//function reference
var fooGreet = foo.Greet;
fooGreet('John Doe');
//apply(binding, args)
fooGreet.apply(foo, ['John Doe'])
fooGreet.apply(bar, ['John Doe'])
// call(binding, *args)
fooGreet.call(foo, 'John Doe')
//================================================//
/*
new
It creates a new object. The type of this object, is simply object.
It sets this new object's internal, inaccessible, [[prototype]] property to be the constructor function's external, accessible, prototype object.
It executes the constructor function, using the newly created object whenever this is mentioned.
*/
var foo = new Person('foo')
/////
var foo = {};
Person.apply(foo, ['foo'])
foo.Greet('John Doe')
//Inheritance
var Animal = function(name){
this.Name = name;
this.Walk = function(){
console.log("I'm "+ this.Name +" I keep Walking now..");
}
}
// prototype inheritance
var Cat = function(name){
this.Name = name;
}
Cat.prototype = new Animal();
var snowBell = new Cat('Snow Bell');
snowBell.Walk();
//Mixin style
var Cat = function(name){
Animal.apply(this, [name]);
}
var snowBell = new Cat('Snow Bell');
snowBell.Walk();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment