SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Strange JS Syntax with example.

    i am editing a JS file i got from the web and i came across some sytax that i havnt seen before in JavaScript. Here is a snippit.

    Code:
    // TreeController
    // ----------------------
    
    	function TreeController(list) {
    		this.list = list;
    		EventListener.addEvent(list, 'click', this.scope('handleClick'));
    		EventListener.addEvent(list, 'mousedown', this.scope('startDrag'));
    		EventListener.addEvent(document, 'mousemove', this.scope('drag'));
    		EventListener.addEvent(document, 'mouseup', this.scope('endDrag'));
    	}
    
    	TreeController.prototype = {
    		scope:function(method) {
    			var scope = this;
    			return function() {
    				scope[method].apply(scope, arguments);
    			}
    		},
    
    		handleClick:function(e) {
    			var target = e.target || e.srcElement;
    			if(/^li$/i.test(target.nodeName)) {
    				if(ClassName.hasClass(target, 'folder'))
    					this.toggle(target);
    			}
    		},
    		
    		toggle:function(li) {
    			var ul = li.getElementsByTagName('ul')[0];
    			if(ClassName.hasClass(li, 'open')) {
    				ClassName.removeClass(li, 'open');
    				ul.style.display = 'none';
    			} else {
    				ClassName.addClass(li, 'open');
    				ul.style.display = 'block';
    			}
    		},
                    // goes on like this for a bit.....
    Anyone know what the : operator is for. Like for example, where it says scope:function(method) or handleClick:function(). And whats with the comma after the brace, like this }, i never saw that before either.

    I know about OO and such, just havnt seen this type of syntax in JS before, any help would be much appreciated.

  2. #2
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That actually looks like the Prototype JS library. How classes are structured in Prototype is different than standard javascript (in fact, the prototype library was created with this functionality in mind). This was done to make javascript class creation closer to traditional class layouts (as seen in Java, C++, PHP, Python, Perl, etc.). See the site link for more information.

    The colon is Javascript Object Notation.

  3. #3
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the JS file i have dosnt use prototype or any other librarys. Is the JSON syntax built into normal javascript?

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,800
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    : is a standard part of Javascript - whatever precedes the : is a label or property name for what follows.

    Common examples include:

    switch (x) {
    case 1: alert('x = 1'); break;
    default:
    }

    <a href="javascript:somecode();">

    and the one being used in your code

    object = {key:value, key1:value1} // which creates object.key and object.key1 and assigns values to them both
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •