I am relatively new to JavaScript and keep seeing .extend and .prototype in third party libraries I am using. I thought it had to do with the Prototype javascript library, but I am beginning to think that is not the case. What are these used for?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
The
extend
method for example in jQuery or PrototypeJS, copies all properties from the source to the destination object.Now about the
prototype
property, it is a member of function objects, it is part of the language core.Any function can be used as a constructor, to create new object instances. All functions have this
prototype
property.When you use the
new
operator with on a function object, a new object will be created, and it will inherit from its constructorprototype
.For example:
Javascript inheritance seems to be like an open debate everywhere. It can be called "The curious case of Javascript language".
The idea is that there is a base class and then you extend the base class to get an inheritance-like feature (not completely, but still).
The whole idea is to get what prototype really means. I did not get it until I saw John Resig's code (close to what
jQuery.extend
does) wrote a code chunk that does it and he claims that base2 and prototype libraries were the source of inspiration.Here is the code.
There are three parts which are doing the job. First, you loop through the properties and add them to the instance. After that, you create a constructor for later to be added to the object.Now, the key lines are:
You first point the
Class.prototype
to the desired prototype. Now, the whole object has changed meaning that you need to force the layout back to its own one.And the usage example:
Read more about it here at Javascript Inheritance by John Resig 's post.
Javascript's inheritance is prototype based, so you extend the prototypes of objects such as Date, Math, and even your own custom ones.
In the snippet above, I define a method for all Date objects ( already existing ones and all new ones ).
extend
is usually a high level function that copies the prototype of a new subclass that you want to extend from the base class.So you can do something like:
And the
Fighter
constructor/object will inherit the prototype ofHuman
, so if you define methods such aslive
anddie
onHuman
thenFighter
will also inherit those.Updated Clarification:
"high level function" meaning .extend isn't built-in but often provided by a library such as jQuery or Prototype.
.extend()
is added by many third-party libraries to make it easy to create objects from other objects. See http://api.jquery.com/jQuery.extend/ or http://www.prototypejs.org/api/object/extend for some examples..prototype
refers to the "template" (if you want to call it that) of an object, so by adding methods to an object's prototype (you see this a lot in libraries to add to String, Date, Math, or even Function) those methods are added to every new instance of that object.Some
extend
functions in third party libraries are more complex than others. Knockout.js for instance contains a minimally simple one that doesn't have some of the checks that jQuery's does: