佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 2368|回复: 23

Javascript

  [复制链接]
发表于 10-4-2011 01:32 AM | 显示全部楼层 |阅读模式
本帖最后由 偶是双子座 于 11-10-2011 01:17 AM 编辑

小弟我平时在公司有空的时候都会研究研究javascript,用javascript配合html和css来写一些简单的程式。一直都很想开一个javascript的讨论贴,和大家一起研究和分享javascript的技术,用法,心得等等。。
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 10-4-2011 01:33 PM | 显示全部楼层
本帖最后由 偶是双子座 于 13-6-2011 11:47 PM 编辑

虽然现在有很多javascript frameworks,但是有时候有些简单和常用的程式在javascript framework里面找不到。。
在这里先和大家分享下一些我常用的程式。希望大家也来分享下各自常用的程式

foreach
javascript没有foreach,每次要用for in来loop,然后每次都用i, j等的counter代码,如果太多nested for有时候会搞到有点乱水。。
在jQuery里面有一个.each的function,不过只限于array的用途,如果要用在json或其他object就不行了。所以就写了以下的程式。

源码:

  1. /*
  2. * mock foreach
  3. * problem: unable to perform break, continue and return operation. use "for in" instead.
  4. */
  5. function foreach( obj, callback ) {
  6.   for ( var i in obj ) {
  7.     var result = callback.call( this, obj[ i ], i );
  8.     if ( result !== undefined ) { //to break the foreach
  9.       return result;
  10.     };
  11.   }
  12. }
复制代码
用法:

  1. var meter = { "mm" : 0.001, "cm": 0.01, "m" : 1, "km" : 1000 };

  2. foreach( meter, function( factor, name ) {
  3.   console.log( "5m == " + ( 5 / factor ) + name );
  4. } );

  5. /* output:
  6. 5m == 5000mm
  7. 5m == 500cm
  8. 5m == 5m
  9. 5m == 0.005km
  10. */
复制代码
回复

使用道具 举报

 楼主| 发表于 10-4-2011 02:38 PM | 显示全部楼层

===, !== 和 ==,!= 的分别

本帖最后由 偶是双子座 于 3-6-2011 11:20 AM 编辑

一般上我们都比较长用到 == 和 != 来比较两个variables。 偶尔会看到 === 或 !==,到底它们的分别在于哪里呢?我不清楚大家上课的时候有没有教到这个,不过我的学院教的没有包裹这个,所以当我第一次遇到的时候就觉得很奇怪 。所以如果你学过,知道了,也不必奇怪为什么这么简单的东西也拿出来分享

其实 === 和 == 之间的分别很简单。== 比较 value;=== 比较 value 和 type。
看下以下的例子应该不难理解:

  1. console.log( 0 == false ); // true
  2. console.log( 0 === false ); // false
  3. console.log( "" == false ); // true
  4. console.log( "" === false ); // false
  5. console.log( 0 == "" ); // true
  6. console.log( 0 === "" ); // false
  7. console.log( 1 == "1" ); // true
  8. console.log( 1 === "1" ); // false
  9. console.log( null == undefined ); // true
  10. console.log( null === undefined ); // false

  11. console.log( 0 === 0 ); // true
  12. console.log( "" === "" ); // true
  13. console.log( null === null ); // true
  14. console.log( undefined === undefined ); // true
复制代码
所以当我们要比较 value 和 type又或者要很确定的比较那variables时,就必须用到===或!==

另外,有意点要提醒大家的。switch是用===来做比较的。
  1.      
  2. function test( val ) {
  3.   switch ( val ) {
  4.     case 1 :
  5.       console.log( "1 numeric" );
  6.       break;
  7.       
  8.     case "1" :
  9.       console.log( "1 text" );
  10.       break;

  11.     case 0 :
  12.       console.log( "0" );
  13.       break;

  14.     case "" :
  15.       console.log( "empty string" );
  16.       break;

  17.     case null :
  18.       console.log( "null" );
  19.       break;

  20.     case undefined :
  21.       console.log( "undefined" );
  22.       break;

  23.     case false :
  24.       console.log( "false" );
  25.       break;

  26.     case true :
  27.       console.log( "true" );
  28.       break;

  29.     default :
  30.       console.log( "none of above" );
  31.       break;
  32.   }
  33. }

  34. test( 1 ); // 1 numeric
  35. test( "1" ); // 1 text
  36. test( true ); // true
  37. test( 0 ); // 0
  38. test( "0" ); // none of above
  39. test( "" ); // empty string
  40. test( false ); // false
  41. test( null );  // null
  42. test( undefined ); // undefined
复制代码
回复

使用道具 举报

发表于 10-4-2011 04:23 PM | 显示全部楼层
多谢分享。
等我整理好了,我再来分享我的心得。
回复

使用道具 举报

 楼主| 发表于 10-4-2011 10:40 PM | 显示全部楼层
多谢分享。
等我整理好了,我再来分享我的心得。
MKIIX 发表于 10-4-2011 04:23 PM

期待大家的分享,这样我们才进步得更快
如果发现我的分享有错误,或有更好的方法,也请记得要说出来,纠正我。
回复

使用道具 举报

 楼主| 发表于 11-4-2011 12:01 AM | 显示全部楼层

加速coding的小提示。 ? : 和 || 的应用

本帖最后由 偶是双子座 于 11-4-2011 01:46 AM 编辑

“? :” 的应用相信大家都不陌生。如果你没用过,不清楚,没关系,现在我和你分享下
?:基本上就像if else一样,只是缩短了写法。
  1. condition ? statement execute when condition true : statement execute when condition false
复制代码
比如:

  1. if ( name == "john" ) {
  2.   console.log( "is john" );
  3. } else {
  4.   console.log( "is not john" );
  5. }

  6. //可以写成
  7. name == "john" ? console.log( "is john" ) : console.log( "is not john" );

  8. //或者
  9. console.log( name == "john" ? "is john" : "is not john" );
复制代码
==========================================================

|| 比较常见于比较两个variables,相等与 OR 的意思。在比较两个variables的同时,|| 其实会return一个value的。这是什么意思呢?请看以下例子。

例子:

  1. var a;
  2. var b;
  3. var c = 0;
  4. var d = 1;
  5. var e = 2;
  6. console.log( 0 || 1 ); // 1
  7. console.log( a || b ); // undefined
  8. console.log( a || c ); // 0
  9. console.log( a || d ); // 1
  10. console.log( d || e ); // 1
  11. console.log( e || d ); // 2
复制代码
在上面的例子里面,可以看见这种情况:当 ( value1 || value2 ) 时,如果value1是true(即不是 false, null, undefined, 0, "" )的话,它就会return value1;如果value1是false(即 false, null, undefined, 0, "" )的话,它就会return value2。

了解了这个,我们就可以应用这一点。

如:

  1. function fn( name ) {
  2.   name = name || "john"; //如果call fn时,有pass name进来的话,name 就会 = name。如果没有,name 就会 = "john"
  3. }
  4. /* //相等与这样的写法:
  5. function fn( name ) {
  6.   if ( name ) {
  7.     name = name;
  8.   } else {
  9.     name = "john";
  10.   }
  11. }
  12. */
复制代码
回复

使用道具 举报

Follow Us
 楼主| 发表于 11-4-2011 09:47 PM | 显示全部楼层

in

本帖最后由 偶是双子座 于 11-4-2011 09:52 PM 编辑

in 除了用于for in之外,还可以用在检查object里面是否有某个property或者method。

用法:

  1. var meter = { "mm" : 0.001, "cm": 0.01, "m" : 1, "km" : 1000 };

  2. console.log( "m" in meter ); // true
  3. console.log( "cm" in meter ); // true
  4. console.log( "tm" in meter ); // false
复制代码
if ( property in object ) VS if ( object.property )
当你要确定某个property是否存在于某个object时,如果用“if ( object.property )”的话,就要注意了,因为如果object.property是 0, "", false, null 和 undefined 的话,if的condition就会是false了。
回复

使用道具 举报

发表于 16-4-2011 12:44 AM | 显示全部楼层
你遗漏了最重要的... 但看了你的代码你应该已经知道coding style的重要性了。

var x = false;

if(x != false)
{
console.log("something is wrong");
}


if(x != false){
console.log("something is wrong");
}

的分别...

其他语言没什么关系但Javascript本质上会自动完成一句syntax.
回复

使用道具 举报


ADVERTISEMENT

发表于 16-4-2011 01:09 AM | 显示全部楼层
本帖最后由 chrizyuen2 于 16-4-2011 01:10 AM 编辑

回复 12# megablue

http://stackoverflow.com/questions/5107510/javascript-open-brace-in-the-same-line
原来如此,谢谢.
回复

使用道具 举报

发表于 16-4-2011 01:45 AM | 显示全部楼层
回复  megablue


原来如此,谢谢.
chrizyuen2 发表于 16-4-2011 01:09 AM



不是反话吧
我分享一下我的学习心得吧...

学Javascript 最重要的步骤是... 要一直学习... 很多时候你学的都是错但自以为学会/对了。

我一开始也是东学一点西学一点... 最重要的是让自己有兴趣学下去。

后来ajax开始盛行我才发觉自己学的懂得太少了就决定重新学起。读了不少javascript的书籍,其中一本最有印象的就是Javascript Bible (没记错应该是Fifth Edition)。

也看了不少的Javascript tech talks... 其中印象最深的有Douglas Crockford (JSON 发明者) 和 John Resig (jquery 发明者).

一直学习。。。纠正自己的错误观念... 再一直学...
javascript 可以说是最被看轻的编程语言;但也同样的最强大的语言。
回复

使用道具 举报

发表于 16-4-2011 05:24 PM | 显示全部楼层
很少写JABASCRIPT。。。呵呵。。多数是在网上下载代码,或者用jquery.
回复

使用道具 举报

 楼主| 发表于 16-4-2011 07:21 PM | 显示全部楼层
你遗漏了最重要的... 但看了你的代码你应该已经知道coding style的重要性了。

var x = false;

if(x ! ...
megablue 发表于 16-4-2011 12:44 AM

的确,coding的习惯也是蛮重要的一部分,除了让code容易阅读之外,可以避免犯下一些不该犯的错误。除了上述的一个例子还有别的,向json object里面的名字:

  1. var obj = {
  2.   "name" : "value" //正确的写法
  3. }

  4. //vs

  5. var obj = {
  6.   name : "value" //虽然说这样写也没问题,但是尽量在name加上""才是正确的写法。因为name不限制于单字。数目,keywords,多个word都可以应用。
  7. }
复制代码
另外,其实我觉得我漏了很多的东西,所以才开这个贴让大家研究和讨论。

我很少看书,很懒惰看书。。通常都是看别人写的code,上网google和看别人的教学来学习的。在学院里面学的Programming真的很皮毛而已,很多东西都要自己学习,免不了遗漏了许多重要的东西。

很少写JABASCRIPT。。。呵呵。。多数是在网上下载代码,或者用jquery.
Mr.Ling 发表于 16-4-2011 05:24 PM

其实如果只是一般的运用是没什么关系啦,了解基本的写法就ok咯。但是如果要写一些像jquery或其他framework或plug-in那样的东西,一定要对javascipt有相当的了解。
现在很多的都是拿开源的code来用就是了,不过我比较想知道他们如何写出这么厉害的东西,有时候会研究他们的code,了解他们的原理,从中学习。
回复

使用道具 举报

发表于 18-4-2011 03:00 PM | 显示全部楼层
本帖最后由 chrizyuen2 于 18-4-2011 03:47 PM 编辑
  1. <option style="font-
  2. weight:bold">blah</option>
复制代码

Chrome:FAIL, Firefox:OK, Opera:FAIL

  1. <option style="color:red">blah</option>
复制代码
All browser:OK

老实说,我真的被吓倒。
回复

使用道具 举报

 楼主| 发表于 18-4-2011 03:12 PM | 显示全部楼层
All browser:OK

老实说,我真的被吓倒。
chrizyuen2 发表于 18-4-2011 03:00 PM


抱歉,不明白你要表达的东西。。
回复

使用道具 举报

发表于 18-4-2011 03:49 PM | 显示全部楼层
回复 18# 偶是双子座

修好了。
回复

使用道具 举报

 楼主| 发表于 18-4-2011 04:19 PM | 显示全部楼层
回复 17# chrizyuen2

这个我倒没有试过。貌似只能换颜色和背景。
另外,请尽量讨论有关javasript的话题吧。
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 3-6-2011 11:11 AM | 显示全部楼层

Ten Oddities And Secrets About JavaScript (转载自Smashing Magazine)

简介:
1. Null is an Object
2. NaN is a Number
3. An Array With No Keys == False (About Truthy and Falsy)
4. replace() Can Accept a Callback Function
5. Regular Expressions: More Than Just Match and Replace
6. You Can Fake Scope
7. Functions Can Execute Themselves
8. Firefox Reads and Returns Colors in RGB, Not Hex
9. 0.1 + 0.2 !== 0.3
10. Undefined Can Be Defined

全文按这里:Smashing Magazine Ten Oddities And Secrets About JavaScript (转载自Smashing Magazine)

Smashing Magazine是一个不错的网站,经常会分享一些有用的IT资讯。
回复

使用道具 举报

 楼主| 发表于 14-6-2011 12:09 AM | 显示全部楼层

Class and Sub Classes

本帖最后由 偶是双子座 于 14-6-2011 06:44 PM 编辑

有跟帖的朋友可能会留意到之前贴过的一些帖子不见了,因为我最近从新修改着一些常用的code,所以拿掉了一些我个人觉得写得不完整的。

最近钻研着John Resig一本叫做"Secrets of the JavaScript Ninja"的书。里面很多很advanced的javascript用法,喜欢javascript的朋友不妨去找来看看。不过,这本书是还不完整的,作者还在编辑中。

参考了这一本书和一些网上的例子,我从新编辑过之前贴过的简单的JQuery Class Plugin。之前的版本太过简单,而且存在着一个问题,就是subclass instanceof superclass的时候是不正确的,现在这个版本纠正了这个问题,也比起之前的多了一些功能,源码也比较复杂。暂时来说,这个版本还不是很完整,还在测试中。。

先来分享源码,已经upload去jquery plugin( http://plugins.jquery.com/project/jClass ),迟些再贴应用的方法。。

  1. /*!
  2. * Simple Class and SubClass handler ( optional for jquery plugin )
  3. *
  4. * Copyright (c) 2011, Meng Soon
  5. * Licensed under MIT
  6. * http://mengsoon.wordpress.com/license
  7. *
  8. * version: 0.3.2
  9. *
  10. */
  11. ( function( window, undefined ) {
  12.   
  13.   //get jQuery from window if it exist
  14.   var $ = window.jQuery ? window.jQuery : {};
  15.   
  16.   //use jquery each or an alternative
  17.   $.each = $.each || function( object, callback ) {
  18.     //loop each items in object
  19.     for ( var index in object ) {
  20.       //call callback function
  21.       callback.call( object, index, object[ index ] );
  22.     }
  23.   }
  24.   
  25.   //use jquery extend or an alternative. jquery before 1.0.4, $.extend is not reliable
  26.   $.extend = ( $.extend && !( ( /^1\.0\..*$/.test( $.fn.jquery ) ) || ( /^\$.*\$/.test( $.fn.jquery ) ) ) ? $.extend : undefined ) || function( deep, target ) {
  27.     var objects;
  28.    
  29.     if ( Object.prototype.toString.call( deep ) != '[object Boolean]' ) {
  30.       //bind false to deep and recall function if first parameter is not boolean
  31.       return arguments.callee.apply( this, [ false ].concat( Array.prototype.slice.call( arguments ) ) );
  32.     } else if ( !( ( objects = Array.prototype.slice.call( arguments, 2 ) ).length ) ) {
  33.       //bind $ to target, bind target to third parameter and recall function if no third parameter exist
  34.       return arguments.callee.call( this, deep, $, target );
  35.     }
  36.    
  37.     //loop for each parameters start from 3rd
  38.     $.each( objects, function( index, object ) {
  39.       //loop each properties
  40.       $.each( object, function( index, value ) {
  41.         //replace value
  42.         target[ index ] = typeof value == 'object' && deep ? arguments.callee.call( this, deep, target[ index ], value ) : value;
  43.       } );
  44.     } );
  45.    
  46.     //return updated target
  47.     return target;
  48.   }
  49.   
  50.   //extend jquery utility
  51.   $.extend( {
  52.     //use jquery isFunction or an alternative
  53.     isFunction : $.isFunction || function( fn ) {
  54.       return Object.prototype.toString.call( fn ) == '[object Function]';
  55.     },
  56.    
  57.     //use jquery isArray or an alternative
  58.     isArray: $.isArray || function( array ) {
  59.       return Object.prototype.toString.call( array ) == '[object Array]';
  60.     }
  61.   } );
  62.   
  63.   //main function
  64.   var jClass = ( function() {
  65.    
  66.     //private cunstruct class indicator, prevent init class execute when inherit from parent on class construction
  67.     var construct = false;
  68.    
  69.     return function( structure, Parent ) {
  70.       //default parent to Object if not pass in
  71.       Parent = Parent || Object;
  72.       
  73.       //set each method's _super to call parent's method
  74.       $.each( structure, function( index, method ) {
  75.         //filter by function
  76.         if ( $.isFunction( method ) ) {
  77.           //set the method's _super to call the parent's method
  78.           //ensure always call the latest version of the parent method
  79.           method._super = function() {
  80.             //get the method
  81.             var fn = Parent.prototype[ index ];
  82.             
  83.             //call and return the method
  84.             //return the _super object if not a function, or probably 'undefined'
  85.             return $.isFunction( fn ) ? fn.apply( this, arguments ) : fn;
  86.           }
  87.         }
  88.       } );
  89.       
  90.       //generate a class
  91.       function Class() {
  92.         //prevent execute of parent's init when new class is constructing
  93.         //check is init a function
  94.         if ( !construct && $.isFunction( this.init ) ) {
  95.           //call init function
  96.           this.init.apply( this, arguments );
  97.         }
  98.       }
  99.       
  100.       //inherit from parent
  101.       construct = true;
  102.       Class.prototype = new Parent();
  103.       construct = false;
  104.       
  105.       //extend Class.prototype to override methods and properties
  106.       $.extend( Class.prototype, structure, {
  107.         
  108.         //override constructor
  109.         constructor: Class,
  110.         
  111.         //create a general _super to call parent's method
  112.         //use parent's _super if exist, prevent redundant
  113.         _super: Class.prototype._super || function() {
  114.           //get the method's _super function created above
  115.           var fn = arguments.callee.caller._super;
  116.          
  117.           //call and return the method
  118.           //return the _super object if not a function, or probably 'undefined'
  119.           return $.isFunction( fn ) ? fn.apply( this, arguments ) : fn;
  120.         }
  121.         
  122.       } );
  123.       
  124.       //extebd class object, add additional utilities
  125.       $.extend( Class, {
  126.         
  127.         //make this class extandable
  128.         extend: ( function( fn ) {
  129.           //return a new function with binded parent class
  130.           return function( structure ) {
  131.             //execute the function with current class as parent
  132.             return fn.call( this, structure, Class );
  133.           };
  134.         //pass in the main function
  135.         } )( arguments.callee ),
  136.         
  137.         //allow to attach new method and override existing method in a class
  138.         //this function will cater for the method calling _super
  139.         method: function( method, fn, overload ) {
  140.         
  141.           ( function( c ) {
  142.          
  143.             //check is method exist and overload is true
  144.             if ( method in c && overload === true ) {
  145.               //overload method
  146.               Class.overload( method, fn );
  147.             } else {
  148.               //set the _super, if existing method exist, use the existing _super instead
  149.               fn._super = $.isFunction( c[ method ] ) ? c[ method ]._super : function() {
  150.                 //get the method
  151.                 var fn = Parent.prototype[ method ];
  152.                
  153.                 //call and return the method
  154.                 //return the _super object if not a function, or probably 'undefined'
  155.                 return $.isFunction( fn ) ? fn.apply( this, arguments ) : fn;
  156.               };
  157.               
  158.               //override method
  159.               c[ method ] = fn;
  160.             }
  161.             
  162.           } )( Class.prototype );
  163.         },
  164.         
  165.         //allow the class overload methods
  166.         //NOTE: only able to overload with different numbers of parameter
  167.         overload: function( method, fn ) {
  168.           //cache original method
  169.           var old = Class.prototype[ method ];
  170.          
  171.           //set the new method _super
  172.           fn._super = old && old._super;
  173.          
  174.           //replace and return new method
  175.           return Class.prototype[ method ] = function() {
  176.          
  177.             //call matched method and return result
  178.             return fn.length == arguments.length ?
  179.             
  180.               //call new function if parameters length match
  181.               fn.apply( this, arguments ) :
  182.               //check is original method is a function, prevent error
  183.               $.isFunction( old ) ?
  184.               
  185.                 //call original method
  186.                 old.apply( this, arguments ) :
  187.                 //return old if not match
  188.                 old;
  189.           };
  190.         },
  191.         
  192.         //multiple overload
  193.         overloads: function( methods ) {
  194.           //go through each methods
  195.           for ( var method in methods ) {
  196.             //list of functions
  197.             var fns = methods[ method ];
  198.             
  199.             //convert to array if it is not array
  200.             fns = $.isArray( fns ) ? fns : [ fns ];
  201.             
  202.             //overload method with each fn
  203.             $.each( fns, function( index, fn ) {
  204.               Class.overload( method, fn );
  205.             } );
  206.           }
  207.         },
  208.         
  209.         //allow the class implement interfaces
  210.         //extend a class with another class or object
  211.         implement: function() {
  212.           //get all pass in arguments
  213.           var interfaces = Array.prototype.slice.apply( arguments );
  214.          
  215.           //process each interface
  216.           $.each( interfaces, function( index, object ) {
  217.             //make sure is not undefined
  218.             if ( object !== undefined ) {
  219.               //create new object is it is a class
  220.               object = object.prototype ? new object() : object;
  221.               
  222.               //extend class's prototype
  223.               $.extend( Class.prototype, object, {
  224.                 //prevent constructor and _super to be overrided
  225.                 constructor: Class.prototype.constructor,
  226.                 _super: Class.prototype._super
  227.               } );
  228.             }
  229.           } );
  230.         }
  231.         
  232.       } );
  233.       
  234.       //return the class function
  235.       return Class;
  236.     }
  237.   } )();
  238.   
  239.   //provide public access
  240.   if ( window.jQuery ) {
  241.     //extend to jquery library if exist
  242.     $.extend( {
  243.       jClass: jClass
  244.     } );
  245.   } else {
  246.     //or else set Class to a global Class
  247.     window.jClass = jClass;
  248.   }
  249.   
  250. } )( window );
复制代码
回复

使用道具 举报

 楼主| 发表于 14-6-2011 06:48 PM | 显示全部楼层
上面贴的源码已经更新过了。


Features:
- Provided easy access to superclass method by simply call 'this._super()'.
- Support for multiple levels of subclasses.
- Class can be easily extended.
- Support for overloads methods and implements interfaces.

用法:

Class creation

  1. var Person = $.jClass( {
  2.   //init will auto call when a new object of the class is initialize
  3.   init: function( name ) {
  4.     this.name = name;
  5.   },

  6.   greet: function() {
  7.     return "Hi, I'm " + this.name ;
  8.   }
  9. } );

  10. var john = new Person( 'John' );

  11. alert( john.greet() ); //Hi, I'm John
复制代码


Extend Class

  1. var Ninja = $.jClass( {
  2.   greet: function() {
  3.     return 'Yihaaa!!';
  4.   }
  5. }, Person );

  6. var resig = new Ninja( 'Resig' );

  7. alert( resig.greet() ); //Yihaaa!!
复制代码


Alternative way to extend class

  1. var Samurai = Person.extend( {
  2.   greet: function() {
  3.     return 'Ha!';
  4.   }
  5. } );

  6. var mike = new Samurai( 'Mike' );

  7. alert( mike.greet() ); //Ha!
复制代码


Access superclass method

  1. var Shogun = Person.extend( {
  2.   greet: function() {
  3.     //or you can access by just call this._super()
  4.     return this._super.apply( this, arguments ) + ". I'm the shogun";
  5.   }
  6. } );

  7. var bob = new Shogun( 'Bob' );

  8. alert( bob.greet() ); //Hi, I'm Bob. I'm the shogun
复制代码


Multiple level of subclass calling super class method

  1. var GreatShogun = Shogun.extend( {
  2.   greet: function() {
  3.     return this._super.apply( this, arguments ) + ". The Great Shogun";
  4.   }
  5. } );

  6. var smith = new GreatShogun( 'Smith' );

  7. alert( smith.greet() ); //Hi, I'm Smith. I'm the shogun. The Great Shogun
复制代码


Implement interfaces

  1. Person.implement( {
  2.   walk: function() {
  3.     return this.name + ' is walking';
  4.   },
  5.   
  6.   run: function() {
  7.     return this.name + ' is running';
  8.   }
  9. } );

  10. alert( john.walk() ); //John is walking
  11. alert( resig.run() ); //Resig is running
  12. alert( bob.run() ); //Bob is running
  13. alert( smith.run() ); //Smith is running
复制代码


Overloads method

  1. Person.overload( 'greet', function( name ) {
  2.   return 'Hi ' + name;
  3. } );

  4. alert( john.greet() ); //Hi, I'm John
  5. alert( john.greet( 'Resig' ) ); //Hi Resig
  6. alert( bob.greet( 'John' ) ); //Hi John. I'm the shogun
  7. alert( smith.greet( 'John' ) ); //Hi John. I'm the shogun. The Great Shogun
复制代码


Overloads multiple methods

  1. Person.overloads( {
  2.   run: [ function( suffix ) {
  3.     return this.run() + ' ' + suffix;
  4.   }, function( prefix, suffix ) {
  5.     return prefix + ' ' + this.run( suffix );
  6.   } ],
  7.   
  8.   walk: function( suffix ) {
  9.     return this.walk() + ' ' + suffix;
  10.   }
  11. } );

  12. alert( john.walk( 'slowly' ) ); //John is walking slowly
  13. alert( resig.run( 'fast' ) ); //Resig is running fast
  14. alert( bob.run( 'Looks, ', 'very fast' ) ); //Looks, Bob is running very fast
复制代码


Modify method without loss the superclass method call

  1. GreatShogun.method( 'greet', function() {
  2.   return this._super.apply( this, arguments ) + ". The Great Great Shogun!!";
  3. } );

  4. alert( smith.greet() ); //Hi, I'm Smith. I'm the shogun. The Great Great Shogun!!
复制代码
回复

使用道具 举报

发表于 14-6-2011 07:04 PM | 显示全部楼层
感謝樓主和各位大大無私分享
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT


本周最热论坛帖子本周最热论坛帖子

ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 12-11-2025 08:11 AM , Processed in 0.128166 second(s), 21 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表