Javascript 如何创建两个类来实现“headbodyinitlast”?
我当前的代码是错误的,但它可能更好地解释了我想做什么,用CoffeeScript编写:Javascript 如何创建两个类来实现“headbodyinitlast”?,javascript,class,oop,coffeescript,Javascript,Class,Oop,Coffeescript,我当前的代码是错误的,但它可能更好地解释了我想做什么,用CoffeeScript编写: implement = (the_class) -> the_class::__defineGetter__ 'now', -> @[@length - 1] the_class::__defineGetter__ 'init', -> @[...(@length - 1)] the_class::__defineGetter__ 'head', -> @[0] the
implement = (the_class) ->
the_class::__defineGetter__ 'now', -> @[@length - 1]
the_class::__defineGetter__ 'init', -> @[...(@length - 1)]
the_class::__defineGetter__ 'head', -> @[0]
the_class::__defineGetter__ 'body', -> @[1..]
class List extends Array
List::__defineGetter__ 'last', -> @[@length - 1]
class Text extends String
implement List
implement Text
a = new Text 'xxx'
console.log a.now
主要是,我想以上面描述的面向对象的方式,将字符串和数组与简单的方法一起使用,比如
head init body tail
。但我不想污染全球范围。我试图从数组和字符串中区分新类,但在我的代码中失败。也许一些解释会有帮助。而试图扩展诸如数组和字符串之类的本机对象通常效果不佳。@muistooshort所以唯一的解决方案就是从头开始创建它们?也许吧。“我真的不确定你在这里想要完成什么。”缪斯托肖特刚才补充了几句话。我想使用像“string”.tail list.head这样的表达式来获取第一个或最后一个元素。这种语法在程序中看起来更好。如果可以实现,我必须使用(head“string”)(tail list)
。为什么不直接将它们添加到数组中。prototype
?