首页 > 代码库 > coffeescript 继承、私有方法、私有属性、公共方法、公共属性、静态方法、静态属性实现
coffeescript 继承、私有方法、私有属性、公共方法、公共属性、静态方法、静态属性实现
class Parent constructor: -> _privateAttr = ‘privateAttr‘ _privateFun = ()-> console.log "Parent::privateFun log #{_privateAttr}" publicAttr: ‘publicAttr‘ publicFun: ()-> _privateFun() console.log "Parent::publicFun log #{@publicAttr} #{_privateAttr}" @staticAttr: ‘staticAttr‘ @staticFun: ()-> console.log "Parent::staticFun log #{@staticAttr}" class Child extends Parent childPublicFun: ()-> try _privateFun()#报错 TypeError:Object #<Parent> has no method ‘_privateFun‘ catch e console.log "_privateFun() is error" console.log "Child::childPublicFun" p = new Parent c = new Child Parent.staticFun() #Parent::staticFun log staticAttr try p.staticFun()#报错 TypeError:Object #<Parent> has no method ‘staticFun‘ catch e console.log "p.staticFun() is error" p.publicFun() #Parent::privateFun log privateAttr #Parent::publicFun log publicAttr privateAttr try p._privateFun()#报错 TypeError:Object #<Parent> has no method ‘_privateFun‘ catch e console.log "p._privateFun() is error" Child.staticFun() #Parent::staticFun log staticAttr c.publicFun() #Parent::privateFun log privateAttr #Parent::publicFun log publicAttr privateAttr c.childPublicFun() #_privateFun() is error #Child::childPublicFun
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。