Upload
sunny-kumar
View
2.132
Download
3
Tags:
Embed Size (px)
DESCRIPTION
What is prototype and how we use it to inherit the properties of an object in JavaScript.
Citation preview
Prototype & Inheritancein
JavaScript
C# Corner – 21st September Sunny Kumar
Prototypes in JavaScript• What is Prototype?• Augmenting the Prototype• Overwriting the Prototype• Use of Prototype• Prototype Chain
Inheritance in JavaScript• Various ways of Inheritance in JavaScript.
- are objects.- returns Value.- can return objects, including other functions.- they have properties.- they have methods.- can be copied, deleted, augmented.- special feature: Invokable.- when invoked with new keyword, returns an object named this , can be modified before it’s returned.
FunctionsSneak Peek into past…
var Person = function(name) { this.name = name; this.getName = function() { return this.name; };};var me = new Person(“Sunny");me.getName(); // “Sunny"
Constructor Functions
Sneak Peek into past…
FireBug Console …as a Learning Tool.
Prototype
Prototype
• prototype is a property of function
object.
• Only function Objects can have prototype property.
Prototype
var foo = function(){}; console.log(foo.prototype);
Object {} //output
Augmenting Prototype(Adding properties to prototype object)
Var foo=function(){};Var myFoo = new foo(); //instantiates an object of foo.foo.prototype.MaxValue=100; foo.prototype.SayHello=function(name){
return “Hi “+name+” !”;
}myFoo.SayHello(“Kumar”); // “Hi Kumar !”
Overwriting Prototype
foo.prototype={x:1,y:2}
Prototype Usage
var Person = function(name) { this.name = name; }; Person.prototype.say = function(){ return this.name; };
Prototype Usage
var dude = new Person('dude');
dude.name; // "dude“
dude.say(); // "dude“ same result
Thank You!