> static - means defined in object's prototype?

No. In class-based languages (which JavaScript isn't, but people still
use the terminology), "static" usually means a method that is global
to a class and not related to specific *instances* of the class. So my
`bar` method was "static" in that sense, because it's not specific to
an instance, it's related to the whole of `C`.

Methods on the prototype are related to instances. They're shared by
all instances derived from the prototype, but they're usually used via
instances. Prototype methods are what `Class.create` creates.

Let's take examples from JavaScript's `String` class (again, it's not
really a class, but people seem to like class-based terminology).
`String` has both class methods (static methods) and instance methods
(on the `String.prototype`). For example, there's
`String.fromCharCode` which is a class method (static): You feed it a
character code, and it gives you a string created using that character
code:

var s = String.fromCharCode(65);
alert(s); // alerts "A", because character code 65 is "A"

`String` objects (instances) have instance methods, such as
`toLowerCase`:

var s = String.fromCharCode(65);
s = s.toLowerCase();
alert(s); // alerts "a"

The functions you use `Class.create` to add to a "class" are instance
methods (like my `foo` example in the `C` class). Those instance
methods are put on the prototype. (JavaScript is sufficiently rich
that instance methods could be on the prototype, or could just be part
of the instance itself -- a concept class-based systems can't handle.)

HTH,
--
T.J. Crowder
Independent Software Engineer
tj / crowder software / com
www / crowder software / com

On May 24, 3:18 pm, buda <[email protected]> wrote:
> static - means defined in object's prototype?
> but what difference between foo, which decleared in prototype, and so
> called static method bar? which is also decleared in prototype?
>
> On 24 май, 16:55, "T.J. Crowder" <[email protected]> wrote:
>
>
>
>
>
>
>
> > On May 24, 2:39 pm, buda <[email protected]> wrote:
>
> > > Why Class.create produces only instance methods but not prototype
> > > ones?
>
> > `Class.create` *does* create methods on the prototype:
>
> > var C = Class.create({
> >   foo: function() {
> >   }
>
> > });
>
> > display("typeof C.prototype.foo == " + typeof C.prototype.foo);
> > // "typeof C.prototype.foo == function"
>
> > Live copy:http://jsbin.com/ajifi5
>
> > Do you mean static methods? E.g., C.methodName? `Class.create` doesn't
> > do them, but they're easy to add either directly or via
> > `Object.extend`:
>
> > Object.extend(C, {
> >   bar: function() {
> >   }
>
> > });
>
> > display("typeof C.bar == " + typeof C.bar);
> > // "typeof C.bar == function"
>
> > Live example:http://jsbin.com/ajifi5/2
>
> > (Side note: I'm using anonymous functions above, which I don't
> > actually like very much.[1] But I didn't want to introduce scoping
> > functions and such.)
>
> > [1]http://blog.niftysnippets.org/2010/03/anonymouses-anonymous.html
>
> > HTH,
> > --
> > T.J. Crowder
> > Independent Software Engineer
> > tj / crowder software / com
> > www / crowder software / com

-- 
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en.

Reply via email to