Please explain difference in ways to construct a javascript editor

D

dennis.sprengers

I've compared some open-source javascript editors, and found different
techniques for constructing the code. Two examples:

CodePress.run = function() {
new CodePress(t);
}

CodePress = function(obj) {
self.initialize = function() {}
self.toggleLineNumbers = function() {}
self.getCode = function() {}
}
-----------------------------------------------
function widgInit() {
new widgEditor(textarea.id)
}

function widgEditor(id) {
this.theToolbar = new widgToolbar(this);
}

function widgToolbar(theEditor) {}
widgToolbar.prototype.addButton = function() {}
widgToolbar.prototype.setState = function() {}
-----------------------------------------------

Could somebody explain to me the difference in the first method, which
defines one object that holds all methods in one class (as I
understand it) and the second method, which uses prototype functions.
I am confused because I thought that prototype functions were only
used to extend methods on strings and arrays. Please enlighten me :)
All help is greatly appreciated.
 
T

Thomas 'PointedEars' Lahn

I've compared some open-source javascript editors, and found different
techniques for constructing the code. Two examples:

CodePress.run = function() {
new CodePress(t);
}

CodePress = function(obj) {
self.initialize = function() {}
self.toggleLineNumbers = function() {}
self.getCode = function() {}
}
-----------------------------------------------
function widgInit() {
new widgEditor(textarea.id)
}

function widgEditor(id) {
this.theToolbar = new widgToolbar(this);
}

function widgToolbar(theEditor) {}
widgToolbar.prototype.addButton = function() {}
widgToolbar.prototype.setState = function() {}
-----------------------------------------------

Could somebody explain to me the difference in the first method, which
defines one object that holds all methods in one class (as I
understand it) and the second method, which uses prototype functions.
I am confused because I thought that prototype functions were only
used to extend methods on strings and arrays. Please enlighten me :)


Much you have to learn, young apprentice.[tm] In your evaluation of the
posted code and the programming language, you are mistaken in several regards:

1. The language as used client-side in Web user agents so far has no
classes, it is an object-oriented language that uses prototype-based
inheritance. Whereas "the language" is actually at least four different
languages, all of them being ECMAScript implementations and therefore
mostly sharing their syntax.

http://PointedEars.de/scripts/es-matrix/

2. CodePress.run() does not make sense as it is a method of a constructor
to create an object with that constructor, but it does not return the
reference to that newly created object which causes it to be eventually
garbage-collected.

3. The assignment to `CodePress' does not make sense in this order as it
would overwrite the previously assigned object reference which would
cause the `run' method to be unavailable and eventually
garbage-collected.

If we ignore that: as indicated by the `initialize' method and the lack
of a proper constructor declaration, that approach appears to use
Prototype.js, a code library that is frowned upon among more competent
developers (and so here) and recommended against because of its utter
lack of acknowledging and using existing language features such as
constructors, requiring language features in a way that makes it
incompatible with older script engines (and so slightly older/less
capable user agents), and being bloated to say the least.

4. widgInit() as it is does not make sense either. There is no need for
another method if there is no testing and only one call which result
is not even returned.

5. widgEditor() as a constructor should by convention have an identifier
that starts with a capital letter.

The OOP in this case would follow the acceptable because reasonable
pattern: the constructor of the object initializes it and constructs the
aggregated objects (which would appear to provide for a back-reference to
the owner as passed in the constructor call). The methods of the object,
as they would require no construction closures, are inherited through the
prototype chain (and not created anew upon every constructor call).

The way the prototype methods are assigned could be improved.
Identifier-reference redundance should be avoided in order to ease
maintenance. Therefore, either the prototype-object should be redefined
with assigning an Object object reference created by literal notation or
a general method should be called that is passed the prototype object
reference and an Object object reference for the properties to be added.

6. Augmentation of built-in prototype objects such as String.prototype and
Array.prototype has side-effects such as making the use if the `in'
operation harder and is therefore generally recommended against.


http://javascript.crockford.com/javascript.html
http://jibbering.com/faq/


HTH

PointedEars
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top