tags:

views:

82

answers:

4

Javascript functions can also be declared prototype like

<object name>.prototype.<variable name>=function(){
//
//
}

How it is different than following declaration

<object name>.<variable name>=function(){
//
//
}

How prototype functions different than normal functions in javascript ?

+4  A: 

functions declared on a base object's prototype are inherited by all instances of that object type.

For example..

String.prototype.foo = function () {
  return 'bar';
};

Now, every string will have the function foo() available.

'test'.foo(); // returns 'bar'

Read more about prototype-based inheritance here

Matt
+1 Also, it is worth explicitly stating that functions *and properties* declared on an object's prototype are inherited by all instances of the that object, *even those that have already been instantiated.*
Justin Johnson
Yes, good point. That last part is very important. The prototype acts somewhat as the last catch before a member is considered 'undefined' .. so modifying the prototype affects existing objects too.
Matt
A: 

It's an OO javascript package that has great built in AJAX functions that utilize the XmlHttpRequest function, DOM manipulation, and JSON data transfer.

The full API documentation is here.

TALLBOY
The OP means `prototype` the keyword, not Prototype the library
Justin Johnson
+2  A: 

Prototype functions are instance functions, whilst normal functions are "static" functions. Functions declared on class's prototype will available on all instances of that class.

var MyClass = function(){
};
MyClass.staticFunction = function(){alert("static");};
MyClass.prototype.protoFunction = function(){alert("instance");};

MyClass.staticFunction(); //OK
MyClass.protoFunction (); //not OK

var myInstance = new MyClass ();
myInstance.staticFunction(); //not OK
myInstance.protoFunction (); //OK
Igor Zevaka
+1 This is the most concise explanation I've seen.
Dr. Frankenstein
A: 

Matt and Igor have already provided enough code samples, but one of the best articles (short, correct and to the point) that you can read is Prototypal Inheritance, by Douglas Crockford.

There are also lots of different ways to facilitate inheritance through popular libraries (Dojo, Prototype, etc)

Justin Johnson