Move Prototype's Function Outside?

V

VUNETdotUS

How do I place a function outside of the, so called, "class" in
prototype.js?

Example: Here is a standard way to write a class using prototype.js.

var Person = Class.create({
initialize: function(name) {
this.name = name;
},
say: function(message) {
return this.name + ': ' + message;
}
});

I want the function "say" to be outside of class like:

var Person = Class.create({
initialize: function(name) {
this.name = name;
},
say : sayOutside // <-------moved out
});

function sayOutside(message) {
return this.name + ': ' + message;
}
 
R

RobG

How do I place a function outside of the, so called, "class" in
prototype.js?

Questions specifically related to features or usage of particular
libraries are off-topic here, you should try a Prototype.js news
group, e.g.:

<URL: http://groups.google.com.au/group/rubyonrails-spinoffs?hl=en&lnk=li
Also, it matters a great deal which version of Prototype.js you are
using - the Class object was changed significantly in version 1.6 in
an attempt to model classic OO inheritance.

You will need to specify whether you want to retain the inheritance
features, or circumvent them.
Example: Here is a standard way to write a class using prototype.js.

var Person = Class.create({
initialize: function(name) {
this.name = name;
},
say: function(message) {
return this.name + ': ' + message;
}

});

I want the function "say" to be outside of class like:

var Person = Class.create({
initialize: function(name) {
this.name = name;
},
say : sayOutside // <-------moved out

});

function sayOutside(message) {
return this.name + ': ' + message;

}

You can use Object.extend, something like:

Object.extend(Person, {say: sayOutside});

but it simply copies properties and values from one objet to another,
which to me just makes it obfuscation and is no different to:

Person.say = sayOutside;

but you may want to use:

Person.prototype.say = sayOutside;

Whatever.
 
V

VUNETdotUS

VUNETdotUS said the following on 12/4/2007 3:27 PM:


You go to one of the prototype forums (search the archives) and ask
someone who supports it how to do what you want.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ -http://jibbering.com/faq/index.html
Javascript Best Practices -http://www.JavascriptToolbox.com/bestpractices/

I'll try their forums. However I thought Prototype is just a JS with
functions and methods to simplify stuff. I wonder how do JS experts
treat prototype anyway?
 
D

David Mark

I'll try their forums. However I thought Prototype is just a JS with
functions and methods to simplify stuff. I wonder how do JS experts
treat prototype anyway?

Like a red-headed stepchild.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top