The prototype

i want to learn javascript recently,now,there is one concepts i can’t understand,which is prototype, in javascript object, i see it show often,and there is also a javascript framwork named prototype. very confused. hope anyone can give me some more datails on the prototype in javascript object

Nice!

a prawn and mayo sandwich

What are we gonna do about them fookin prawns, man? I lol’d.

Does this help?
http://www.room51.co.uk/js/protoMart.html

in the above,how to use an array literal declare to make the foo.length equal to 10.

I forget, cause usually I start with and just fill it with whatever I need to fill it with.
You can prolly declare it:

var foo=;
foo.length = 10;

Ah, this page describes “prototype” for arrays:

why in “var foo = new Array(10);
foo.length;” can’t write like this
foo.prototype.length;

From the link above. So, you can’t say foo.prototype.length, but let’s say you made some arrays and none of them are based on each other, but you want all of them to have a new property, you could

var cat=, dog=, moose=;
so clearly all arrays. You want to add a “fur” property to all arrays in your script, because you know they’re all going to be hairy animals:

Array.prototype.fur = (something);

now there’s cat.fur, dog.fur, and moose.fur. Plus there’s a .fur for any new arrays you make (if I have this correctly).


if (Function.prototype.call) {
listener.call(target, event);
}

i am sorry,i don’t understand this code.

I don’t either, I grabbed it from SitePoint’s Core library which you can use to make sure IE works the same as other browsers regarding “this” and event loaders/listeners. I saw that in the code; it SEEMS to be checking IF the Function base object contains a prototype with “call” in it (maybe checking for an old version of IE that doesn’t have .call?? I dunno).

Another one I’ve seen was Mozilla’s code to make other browsers add indexOf to Arrays:

Under “compatibility” they have code, which I’ve copied too, that adds a method “indexOf” to Array objects.

if (Function.prototype.call) {
listener.call(target, event);
}

i am sorry,i don’t understand this code.

why in “var foo = new Array(10);
foo.length;” can’t write like this
foo.prototype.length;

using new
var foo = new Array(10);

using an array literal:
var foo = ;

in the above,how to use an array literal declare to make the foo.length equal to 10.

The library called “Prototype” was probably badly named, like “Javascript” itself.

The prototypal inheritance is the basis of how one object can automatically have the same properties and/or methods of another object in Javascript, as opposed to the way most other languages do it (they use classes).

You can also manually call out the method called “prototype”.


if (Function.prototype.call) {
    listener.call(target, event);
}

Here it’s looking specifically for the “call” method of the “prototype” method of the Function object. That’s one of the default, built-in objects in Javascript:
Function
Array
Date
RegExp
String
Number
… (there are more)

These are already sitting in Javascript and they are pre-built with their own special methods (functions that are special to them). Technically these all belong ultimately to their “prototype” property, and I’m still new enough not to understand when you need to specifically mention “prototype” by name like that.

But, for example, the Array object has a method called “length”. All Arrays have a .length method, and any time you make a new Array, it’s already got that, as part of the Array object’s prototype. All new Arrays you make, they are inheriting that method from the main Array object, no matter how you actually make your own array.

using new
var foo = new Array(10);

You can now call foo.length, without having to manually add in “length” to foo… it came with foo being an array.

using an array literal:
var foo = ;

Even though it’s empty at this point, it’s still an array. If you asked what TypeOf it was, it would be an Array object. So it has a “length” method by default.

When you make a function
var foo = function () {
};
or
foo: function (){
}
or
var foo = new Function(){}; (not recommended)
or
function foo() {
}

you get the stuff that Function objects have, like “call” and “args”.

Does this make sense?