I have gone through plenty of Stack Overflow question that had description but I seriously found them very confusing. What I want is a simple explanation,please don't refer a link.
I'm totally confused, completely jumbled up between:
Prototypes,
Objects,
Constructor
I have done plenty of research but I found them very complicated.
Any simple explanation??
You're in luck. There's a very simple explanation:
Step One: Create an Object
Say you want to circle:
Step Two: Give it Some Properties
A circle can be drawn, so let's create a property called
draw
:Hence we have a method called
draw
belonging to the objectcircle
.Step Three: Extend an Object
Now I want a ball and a ball is kind of like a circle. So let's extend
circle
to create a ball:circle
and used it to create a new object calledball
.ball
now has all the properties ofcircle
. So we can callball.draw
.circle
is the prototype ofball
.Step Four: Give it Some Properties
Every ball has a
radius
, so let's give ours one:Step Five: Create a Constructor
There's a problem here. Every time I want to create a new ball I extend
circle
and manually define theradius
of the ball. Instead I would like a function to create the ball and give it a radius for me. This function is called a constructor:That's pretty much all you need to know about prototypes, objects and constructors.
Of course there's a lot more explanation but it's too much for one StackOverflow answer. I wrote a blog post on it, and I'm not planning to rewrite the same thing here. You should read my blog. It's worth it: http://aaditmshah.github.io/why-prototypal-inheritance-matters
Edit: Sure, I'll explain what happening in that code: http://cssdeck.com/labs/4ksohwya
First, scroll down to the very end:
When the page loads it executes
init
:The
init
function creates an instance ofGarden
(garden = new Garden();
) and executes theinitialize
method ofgarden
. It also calls the_doit
function in intervals of 30 milliseconds.The
initialize
method ofgarden
then creates some instances ofGrass
, calls theirinitialize
methods and stores them in an array.The
_doit
function calls thepaint
function ofgarden
, and thepaint
function ofgarden
calls thepaint
function of each grass.So here there are two constructors:
Grass
Garden
Here are the two prototypes:
Grass.prototype
Garden.prototype
Inside the
init
function we create a single instance ofGarden
(that's one object):Inside the
initialize
method ofgarden
we create multiple instances ofGrass
:That's it.
I guess I am too late, but here would be my 2 cents:
Prototype:
now every instance of String will have the property x.
So be it
(new String()).x
or"".x
both have the value equal to yahooHence it's like extending a predefined class.
Objects
Everything in JavaScript, except the other primitive types, is an object.
An object is a collection of name-value pairs, nothing more, nothing less.
Even an Array is an object in JS with some additional properties & methods.
Functions & Methods
Here is a function :
Now let's give it the status of a method of an Array:
Constructor:
gets the implementation of String from :
String.constructor
Similarly the code you write in any function goes into this very function.
JavaScript is the scripting language used in web applications to make the web pages dynamic. This language allows to handle user actions by handling events on the elements displayed on the page.... Also it is used to modify the content on the web page based on the condition...
The method (or) function is the programming construct used in javascript for specifying the logic. You will put the your required logic in a method and you will invoke it from the web page upon any event...
There are no constructors in javascript...
There will be implicit and user defined objects in javascript... Implicit objects include Date, Array etc...
This is just a basic abstract.. you need to follow either websites (or) books to know much more about javascript
Okay, a whirlwind tour:
Objects
An object is a thing that has properties. Properties have names and values. The names are always strings (although we can write them without quotes most of the time), and the values can be anything JavaScript supports: Numbers, strings, booleans, null, undefined, or references to objects.
So:
a
is a variable referring to an object, which has a property namedpropertyName
, whose value is the string"property value"
.Prototypes
An object (say,
obj
) can have another object (say,p
) "behind" it, wherep
lendsobj
its properties unlessobj
has its own property with a given name.p
is the prototype object ofobj
. This is easiest to understand with an example:A very, very important aspect of prototypes is that the connection between the objects is live. So if
obj
doesn't have ay
property, every time we askobj
fory
it goes and asksp
. And so if we changep
's value fory
, that change shows up if we askobj
for it:This live connection is a vital thing. So again, think of it like this: We ask
obj
for the propertyy
, andobj
says "I don't have my own, so I'll go askp
for it and give you that."Note: The
Object.create
function I've been using to createobj
is new as ECMAScript5 (the spec update from a couple of years ago). We'll come back to another way to given an object a prototype further down.The prototype for an object is currently always set when the object is created, and cannot be changed (I couldn't swap in a
q
instead ofp
above after creatingobj
). Above I'm usingObject.create
to do it (and below we'll talk about constructor functions). Until ECMAScript5 (ES5), there was no standard way to get the prototype of an object. ES5 gives us a way to do it now, calledObject.getPrototypeOf
, but still doesn't offer a way to change it. The next version, ES6, will take things a bit further.Functions
Functions are units of code that do things. Functions are also objects, and so they can have properties, although in practice it's relatively rare to use properties on functions (other than
call
andapply
, which we don't need to talk about here).You can declare a function:
...or you can create one with an expression:
Declarations and expressions are different. Function declarations are evaluated before any step-by-step code in the same scope is performed. Function expressions are evaluated as they're encountered in the step-by-step code, like all other expressions. (People sometime call this "hoisting" because it means that in effect, even if a function declaration is at the bottom of a scope, it happens as though it had been lifted -- hoisted -- to the top.)
Functions can have arguments:
And they can have return values:
If a function doesn't return something else, the result of calling the function is the value
undefined
.Methods
JavaScript doesn't have methods. It only has functions — but that's all it really needs. But if you have a function assigned to an object property, and you call that function as part of an expression retrieving the property from the object, then something happens that makes JavaScript seem to have methods: The
this
keyword refers to that object within the function call. Again, an example works wonders:More on my blog:
this
Constructors
Constructor functions are used with the
new
keyword, and they're one of the ways you give an object a prototype. When you call a function vianew
, a new object is created, and assigned a prototype from the function'sprototype
property:Every function automatically has a
prototype
property, even though of course we don't use the vast majority of functions as constructors.An important thing to note here: The
prototype
property of a function is just a boring old property. It isn't the prototype of any object until/unless that function is called via thenew
operator. Thenew
operator uses theprototype
property of the function to set the prototype of the new object when you call the function vianew
, but that's all.It's worth mentioning that until ES5, constructor functions like the above were the only way you could create an object with a given prototype. But with ES5, we got
Object.create
, which opened up more patterns for how to use JavaScript. (It was always possible to create your ownObject.create
, by using a temporary function, and in fact that's exactly what some people did.) Some people don't like using thenew
keyword and theprototype
property, they prefer to use a "builder" pattern where you just call a function and get back an object. JavaScript is so flexible that you can do that.More to Explore
I write about JavaScript on my blog: Nifty Snippets -
javascript
labelThe Mozilla Development Network has excellent JavaScript information
There is, of course, the specification — it's the best source for the actual nitty-gritty detail, but it's very hard reading :-)