Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what exactly does the keyword prototype do in jquery?

Is the keyword (or method?) prototype in jquery kind of like extension methods?

i.e. all classes will have this functionality available to it going forward?

like image 859
mrblah Avatar asked Oct 27 '09 02:10

mrblah


People also ask

What is jQuery prototype?

fn is a property of the jQuery object (just like prototype is just a property of the jQuery object). The value of both the fn property and the prototype property is a reference to a single object.

What is prototype keyword in JavaScript?

Every object in JavaScript has a built-in property, which is called its prototype. The prototype is itself an object, so the prototype will have its own prototype, making what's called a prototype chain. The chain ends when we reach a prototype that has null for its own prototype.

What is the difference between __ proto __ and prototype?

The prototype property is set to function when it is declared. All the functions have a prototype property. proto property that is set to an object when it is created using a new keyword. All objects behavior newly created have proto properties.


2 Answers

This is part of javascript and not specific to jquery.

the prototype property defines methods and properties shared by all objects of that type.

e.g.

function MyClass()
{
}

myClass.prototype.myMethod = function()
{
    alert("hello world");
}

var myObject = new MyClass();
myObject.myMethod();

All instances of MyClass will have (share) the method myMethod().

Note that methods on the prototype do not have the same visibility as methods declared within the constructor.

For example:

function Dog(name, color)
{
    this.name = name;

    this.getColor = function()
    {
        return color;
    }
}

Dog.prototype.alertName = function {
    alert(this.name);
}

Dog.prototype.alertColor = function {

    //alert(color);  //fails. can't see color. 
    //alert(this.color); //fails. this.color was never defined

    alert(this.getColor()); //succeeds
}

var fluffy = new Dog("Fluffy","brown");
like image 103
Jonathan Fingland Avatar answered Nov 15 '22 07:11

Jonathan Fingland


prototype is not a jQuery keyword; it is a Javascript keyword. It is used to add public functions to objects in a way such that they will exist every time you create a new instance of that object.

  • http://www.javascriptkit.com/javatutors/proto.shtml
  • http://www.devarticles.com/c/a/JavaScript/Object-Oriented-JavaScript-Using-the-Prototype-Property/
like image 31
Marc W Avatar answered Nov 15 '22 06:11

Marc W