Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / December 2007



Tip: Looking for answers? Try searching our database.

Please explain difference in ways to construct a javascript editor

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
dennis.sprengers@gmail.com - 30 Dec 2007 21:45 GMT
I've compared some open-source javascript editors, and found different
techniques for constructing the code. Two examples:

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

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.
Thomas 'PointedEars' Lahn - 31 Dec 2007 01:04 GMT
> I've compared some open-source javascript editors, and found different
> techniques for constructing the code. Two examples:
[quoted text clipped - 27 lines]
> 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
Signature

var bugRiddenCrashPronePieceOfJunk = (
   navigator.userAgent.indexOf('MSIE 5') != -1
   && navigator.userAgent.indexOf('Mac') != -1
)  // Plone, register_function.js:16

Randy Webb - 31 Dec 2007 01:13 GMT
Thomas 'PointedEars' Lahn said the following on 12/30/2007 8:04 PM:

<snip>

> Much you have to learn, young apprentice.[tm]

<URL: http://en.wikipedia.org/wiki/Idiot>

Signature

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

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.