加入我们,或者,欢迎回来。 您需要 登录  才可以下载或查看,没有帐号?注册会员  
 
x 
 
 本帖最后由 墨凌羽 于 2015-12-1 07:52 编辑  sprite = Sprite.new ( ) 
sprite.bitmap  = bitmap
#一般创建之后还要调整坐标之类的 
sprite.x  = 12 
sprite.y  = 13 
sprite.z  = 100 
sprite = Sprite.new ( ) 
sprite.bitmap  = bitmap
#一般创建之后还要调整坐标之类的 
sprite.x  = 12 
sprite.y  = 13 
sprite.z  = 100 
ClassName.prototype .method .function  ( )  { 
//一些其他部分的代码 
this .sprite  = new  Sprite( bitmap) ;
this .sprite .x  = 12 ;
this .sprite .y  = 13 ;
this .addchid ( this .sprite ) ;
//一些其他代码 
} 
ClassName.prototype .method .function  ( )  { 
//一些其他部分的代码 
this .sprite  = new  Sprite( bitmap) ;
this .sprite .x  = 12 ;
this .sprite .y  = 13 ;
this .addchid ( this .sprite ) ;
//一些其他代码 
} 
cascade  JavaScript语言精粹   Douglas Crockford  )。级联 //一个级联的例子 
var  button = new  Button( ) 
  .move ( 12 ,23 ) 
  .width ( 45 ) 
  .height ( 200 ) 
  .text ( "开始游戏" ) 
  .on ( 'mousedown' , function ( ) { Scene.goto ( MapScene) } ) ;
//一个级联的例子 
var  button = new  Button( ) 
  .move ( 12 ,23 ) 
  .width ( 45 ) 
  .height ( 200 ) 
  .text ( "开始游戏" ) 
  .on ( 'mousedown' , function ( ) { Scene.goto ( MapScene) } ) ;
级联的实现 A = function  ( id)  { 
  this ._id = id;
  this ._x = 0 ;
  this ._y = 0 ;
  this .event  = { } ;
  return  this ;
} 
 
A.prototype .constructor  = A;
 
A.prototype .x  = function  ( val)  { 
  if ( val === undefined)  return  this ._x;
  this ._x = val;
  return  this ;
} 
 
A.prototype .y  = function  ( val)  { 
  if ( val === undefined)  return  this ._y;
  this ._y = val;
  return  this ;
} 
 
A.prototype .method1  = function  ( val)  { 
  this ._x = this ._x + val;
  return  this ;
} 
 
A.prototype .method2  = function  ( val)  { 
  this ._y = this ._y + val;
  return  this ;
} 
 
A.prototype .set_event  = function  ( name , func)  { 
  Object.defineProperty ( this .event , name , { value: func} ) 
  return  this ;
} 
 
a = new  A( 1 ) 
    .x ( 12 ) 
    .y ( 13 ) 
    .method1 ( 12 ) 
    .set_event ( 'click' , function  ( )  { console.log ( a.x ( ) ,a.y ( ) ) } ) 
    .method2 ( 12 ) ;
a.event [ 'click' ] ( ) 
A = function  ( id)  { 
  this ._id = id;
  this ._x = 0 ;
  this ._y = 0 ;
  this .event  = { } ;
  return  this ;
} 
 
A.prototype .constructor  = A;
 
A.prototype .x  = function  ( val)  { 
  if ( val === undefined)  return  this ._x;
  this ._x = val;
  return  this ;
} 
 
A.prototype .y  = function  ( val)  { 
  if ( val === undefined)  return  this ._y;
  this ._y = val;
  return  this ;
} 
 
A.prototype .method1  = function  ( val)  { 
  this ._x = this ._x + val;
  return  this ;
} 
 
A.prototype .method2  = function  ( val)  { 
  this ._y = this ._y + val;
  return  this ;
} 
 
A.prototype .set_event  = function  ( name , func)  { 
  Object.defineProperty ( this .event , name , { value: func} ) 
  return  this ;
} 
 
a = new  A( 1 ) 
    .x ( 12 ) 
    .y ( 13 ) 
    .method1 ( 12 ) 
    .set_event ( 'click' , function  ( )  { console.log ( a.x ( ) ,a.y ( ) ) } ) 
    .method2 ( 12 ) ;
a.event [ 'click' ] ( ) 
class  A
  attr_reader :id 
  attr_accessor :x ,:y
  def  initialize( id) 
    @id  = id
    @x  = 0 
    @y  = 0 
    @event  = { } 
  end 
 
  def  x( x = nil ) 
    return  @x  if  x == nil 
    @x  = x
    return  self 
  end 
 
  def  y( y = nil ) 
    return  @y  if  y == nil 
    @y  = y
    return  self 
  end 
 
  def  method1( x) 
    @x  += x
    self 
  end 
 
  def  method2( y) 
    @y  += y
    self 
  end 
 
  def  event( name = nil , &block) 
    return  @event  if ( name == nil ) 
    @event [ name]  = lambda  &block
    self 
  end 
end 
 
a = A.new ( 1 ) 
  .x ( 12 ) 
  .y ( 15 ) 
  .method1 ( 12 ) 
  .method2 ( 13 ) 
  .event ( :click ) { p  a.x ,a.y } 
a.event [ :click ] .call 
class  A
  attr_reader :id 
  attr_accessor :x ,:y
  def  initialize( id) 
    @id  = id
    @x  = 0 
    @y  = 0 
    @event  = { } 
  end 
 
  def  x( x = nil ) 
    return  @x  if  x == nil 
    @x  = x
    return  self 
  end 
 
  def  y( y = nil ) 
    return  @y  if  y == nil 
    @y  = y
    return  self 
  end 
 
  def  method1( x) 
    @x  += x
    self 
  end 
 
  def  method2( y) 
    @y  += y
    self 
  end 
 
  def  event( name = nil , &block) 
    return  @event  if ( name == nil ) 
    @event [ name]  = lambda  &block
    self 
  end 
end 
 
a = A.new ( 1 ) 
  .x ( 12 ) 
  .y ( 15 ) 
  .method1 ( 12 ) 
  .method2 ( 13 ) 
  .event ( :click ) { p  a.x ,a.y } 
a.event [ :click ] .call 
class  Class 
  def  attr_cascade( *arg) 
    arg.each  do  |i|
    attr_accessor i
      define_method( i)  do  |val = nil|
        return  instance_variable_get( '@' +i.id2name )  if  val == nil 
        instance_variable_set( '@' +i.id2name , val) 
        self 
      end 
    end 
  end 
end 
class  Class 
  def  attr_cascade( *arg) 
    arg.each  do  |i|
    attr_accessor i
      define_method( i)  do  |val = nil|
        return  instance_variable_get( '@' +i.id2name )  if  val == nil 
        instance_variable_set( '@' +i.id2name , val) 
        self 
      end 
    end 
  end 
end 
其他部分 def  sum( *arg) 
  sum = 0 
  arg.each { |i| sum += i} 
  return  sum
end 
def  sum( *arg) 
  sum = 0 
  arg.each { |i| sum += i} 
  return  sum
end 
function  sum( )  { 
  var  sum = 0 ;
  var   length = arguments.length ;
  for  ( var  i = 0 ; i < length; i++)  { 
    sum += arguments[ i] ;
  } 
  return  sum;
} 
function  sum( )  { 
  var  sum = 0 ;
  var   length = arguments.length ;
  for  ( var  i = 0 ; i < length; i++)  { 
    sum += arguments[ i] ;
  } 
  return  sum;
} 
总结