<legend id='R5vS7'><style id='R5vS7'><dir id='R5vS7'><q id='R5vS7'></q></dir></style></legend>

      • <bdo id='R5vS7'></bdo><ul id='R5vS7'></ul>

    1. <i id='R5vS7'><tr id='R5vS7'><dt id='R5vS7'><q id='R5vS7'><span id='R5vS7'><b id='R5vS7'><form id='R5vS7'><ins id='R5vS7'></ins><ul id='R5vS7'></ul><sub id='R5vS7'></sub></form><legend id='R5vS7'></legend><bdo id='R5vS7'><pre id='R5vS7'><center id='R5vS7'></center></pre></bdo></b><th id='R5vS7'></th></span></q></dt></tr></i><div id='R5vS7'><tfoot id='R5vS7'></tfoot><dl id='R5vS7'><fieldset id='R5vS7'></fieldset></dl></div>
    2. <tfoot id='R5vS7'></tfoot>
    3. <small id='R5vS7'></small><noframes id='R5vS7'>

      如何将方法设为私有并在 Coffeescript 中继承?

      时间:2024-04-20

        • <small id='ng2SR'></small><noframes id='ng2SR'>

              <tbody id='ng2SR'></tbody>

              <bdo id='ng2SR'></bdo><ul id='ng2SR'></ul>
                <legend id='ng2SR'><style id='ng2SR'><dir id='ng2SR'><q id='ng2SR'></q></dir></style></legend>
              1. <tfoot id='ng2SR'></tfoot>
                <i id='ng2SR'><tr id='ng2SR'><dt id='ng2SR'><q id='ng2SR'><span id='ng2SR'><b id='ng2SR'><form id='ng2SR'><ins id='ng2SR'></ins><ul id='ng2SR'></ul><sub id='ng2SR'></sub></form><legend id='ng2SR'></legend><bdo id='ng2SR'><pre id='ng2SR'><center id='ng2SR'></center></pre></bdo></b><th id='ng2SR'></th></span></q></dt></tr></i><div id='ng2SR'><tfoot id='ng2SR'></tfoot><dl id='ng2SR'><fieldset id='ng2SR'></fieldset></dl></div>
                本文介绍了如何将方法设为私有并在 Coffeescript 中继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                如何将btnClick"方法设为私有?

                How to make method "btnClick" private?

                class FirstClass
                  constructor: ->
                    $('.btn').click @btnClick
                
                  btnClick: =>
                    alert('Hi from the first class!')
                
                class SecondClass extends FirstClass
                  btnClick: =>
                    super()
                    alert('Hi from the second class!')
                
                @obj = new SecondClass
                

                http://jsfiddle.net/R646x/17/

                推荐答案

                JavaScript 中没有私有,所以 CoffeeScript 中也没有私有.您可以像这样在班级级别将内容设为私有:

                There's no private in JavaScript so there's no private in CoffeeScript, sort of. You can make things private at the class level like this:

                class C
                    private_function = -> console.log('pancakes')
                

                private_function 将只在 C 中可见.问题是 private_function 只是一个函数,它不是 C 实例的方法.您可以使用 Function.apply 解决这个问题 或 Function.call:

                That private_function will only be visible within C. The problem is that private_function is just a function, it isn't a method on instances of C. You can work around that by using Function.apply or Function.call:

                class C
                    private_function = -> console.log('pancakes')
                    m: ->
                        private_function.call(@)
                

                所以在你的情况下,你可以这样做:

                So in your case, you could do something like this:

                class FirstClass
                    btnClick = -> console.log('FirstClass: ', @)
                    constructor: ->
                        $('.btn').click => btnClick.call(@)
                
                class SecondClass extends FirstClass
                    btnClick = -> console.log('SecondClass: ', @)
                

                演示:http://jsfiddle.net/ambiguous/5v3sH/

                或者,如果您不需要 btnClick 中的 @ 是任何特别的东西,您可以按原样使用该函数:

                Or, if you don't need @ in btnClick to be anything in particular, you can just use the function as-is:

                class FirstClass
                    btnClick = -> console.log('FirstClass')
                    constructor: ->
                        $('.btn').click btnClick
                

                演示:http://jsfiddle.net/ambiguous/zGU7H/

                这篇关于如何将方法设为私有并在 Coffeescript 中继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:使用 will_paginate gem 在 Rails 3.2.3 中调用 Ajax 下一篇:随每个骨干网同步请求发送令牌

                相关文章

                <small id='k2Bti'></small><noframes id='k2Bti'>

                <tfoot id='k2Bti'></tfoot>
                    <legend id='k2Bti'><style id='k2Bti'><dir id='k2Bti'><q id='k2Bti'></q></dir></style></legend>
                      <bdo id='k2Bti'></bdo><ul id='k2Bti'></ul>
                  1. <i id='k2Bti'><tr id='k2Bti'><dt id='k2Bti'><q id='k2Bti'><span id='k2Bti'><b id='k2Bti'><form id='k2Bti'><ins id='k2Bti'></ins><ul id='k2Bti'></ul><sub id='k2Bti'></sub></form><legend id='k2Bti'></legend><bdo id='k2Bti'><pre id='k2Bti'><center id='k2Bti'></center></pre></bdo></b><th id='k2Bti'></th></span></q></dt></tr></i><div id='k2Bti'><tfoot id='k2Bti'></tfoot><dl id='k2Bti'><fieldset id='k2Bti'></fieldset></dl></div>