How to call a method from inside an object?

Nate

I'm trying to modify a plugin I found for Redactor to get it working with the newest version, but my ignorance of JavaScript is keeping me from getting it working.

if (!RedactorPlugins) var RedactorPlugins = {};

RedactorPlugins.wordcount = function() {
    return {
        init: function() {
            var self = this;
            var $box = $(this.core.getBox());
            if ($box.length>0) {
                if ($box.find('.redactor-wordcount').length==0) {
                    var $wordcount_holder = $('#counter');

                    $box.on('keyup', function () {
                        $wordcount_holder.text('Words: ' + self.count());
                    });

                    $wordcount_holder.text('Words: ' + self.count());
                }
            }
        },

        count: function() {
            var html = this.get(),
                text = $(html).text().replace(/\t+/g, " ").replace(/\n/g, " ").replace(/\s+/g, " ");
            return text.split(' ').length - 1;
        }
    };
};

When I load the page an error is output saying Uncaught TypeError: undefined is not a function. It's referring to the count function.

I was under the impression this syntax:

return {
...
}

Caused an object to be returned, but for some reason calling self.count() causes the above error to be thrown.

How can I call the count function from inside the init function?

EDIT: AS an aside, here is how Redactor plugins (for the new version of the software) are supposed to be defined:

if (!RedactorPlugins) var RedactorPlugins = {};

RedactorPlugins.myplugin = function()
{
    return {
        myMethod: function()
        {
            // your code
        }
    };
};
AJ Richardson

I suspect that Redactor is calling init() with this pointing to some object that is different than what you expect. In order to call count(), you probably need to declare it before your return statement so you can use it in multiple places.

RedactorPlugins.wordcount = function() {

    // Declare count here, then remove 'this.' when you call it (see my comments below)
    var count = function() {
        var html = this.get(),
            text = $(html).text().replace(/\t+/g, " ").replace(/\n/g, " ").replace(/\s+/g, " ");
        return text.split(' ').length - 1;
    }

    return {
        init: function() {
            var self = this;
            var $box = $(this.core.getBox());
            if ($box.length>0) {
                if ($box.find('.redactor-wordcount').length==0) {
                    var $wordcount_holder = $('#counter');

                    $box.on('keyup', function () {
                        // Note the lack of 'this' here...
                        $wordcount_holder.text('Words: ' + count());
                    });
                    // ... and here
                    $wordcount_holder.text('Words: ' + count());
                }
            }
        },

        // Pass your count function as part of the return value
        // if you still want it to be accessible to whoever uses
        // the return value of this function.
        count: count
    };
};

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Is it bad practice to instantiate an object as an argument inside a method/function call?

분류에서Dev

How to call async method inside a method which has return type?

분류에서Dev

How do I call the first area method from the main method?

분류에서Dev

Call parent method in JavaScript class but stll have access to prototype methods inside object instance?

분류에서Dev

How to call method of wrapped object by unique_ptr?

분류에서Dev

How to call controller method from view RAILS 4.0

분류에서Dev

how to call a user defined method in a custom view from an activity

분류에서Dev

How to call Springs service method from controller (Junit)

분류에서Dev

How do I call a javascript method from angular js?

분류에서Dev

Can't call static method inside class

분류에서Dev

__call__ when calling a method inside the class

분류에서Dev

How to call reflection method

분류에서Dev

Sending over Interface Object from method call hiding implementation super class on server via RMI?

분류에서Dev

Ruby call method on object instead of passing object as argument to method

분류에서Dev

Call controller method from javascript

분류에서Dev

call childrens method from parent

분류에서Dev

angular js call a method for a controller inside another controllers method

분류에서Dev

How to run method that returns an object of "Type" from generic class

분류에서Dev

How to run method that returns an object of "Type" from generic class

분류에서Dev

Java- code in main method or not? If not, how do I call new method from main?

분류에서Dev

Call Object.prototype method on Global Scope

분류에서Dev

call isset from function inside class

분류에서Dev

Angular JS: How to call a function inside link from controller in angular directive

분류에서Dev

How to pass a method as a string and call it

분류에서Dev

Using $this when not in object contex call simple method in static method

분류에서Dev

Class object can only call a method if object attribute is between a range

분류에서Dev

How to populate an object inside an object in an array?

분류에서Dev

How to call the Javascript file inside Body tag

분류에서Dev

How to call web service inside of forloop?

Related 관련 기사

  1. 1

    Is it bad practice to instantiate an object as an argument inside a method/function call?

  2. 2

    How to call async method inside a method which has return type?

  3. 3

    How do I call the first area method from the main method?

  4. 4

    Call parent method in JavaScript class but stll have access to prototype methods inside object instance?

  5. 5

    How to call method of wrapped object by unique_ptr?

  6. 6

    How to call controller method from view RAILS 4.0

  7. 7

    how to call a user defined method in a custom view from an activity

  8. 8

    How to call Springs service method from controller (Junit)

  9. 9

    How do I call a javascript method from angular js?

  10. 10

    Can't call static method inside class

  11. 11

    __call__ when calling a method inside the class

  12. 12

    How to call reflection method

  13. 13

    Sending over Interface Object from method call hiding implementation super class on server via RMI?

  14. 14

    Ruby call method on object instead of passing object as argument to method

  15. 15

    Call controller method from javascript

  16. 16

    call childrens method from parent

  17. 17

    angular js call a method for a controller inside another controllers method

  18. 18

    How to run method that returns an object of "Type" from generic class

  19. 19

    How to run method that returns an object of "Type" from generic class

  20. 20

    Java- code in main method or not? If not, how do I call new method from main?

  21. 21

    Call Object.prototype method on Global Scope

  22. 22

    call isset from function inside class

  23. 23

    Angular JS: How to call a function inside link from controller in angular directive

  24. 24

    How to pass a method as a string and call it

  25. 25

    Using $this when not in object contex call simple method in static method

  26. 26

    Class object can only call a method if object attribute is between a range

  27. 27

    How to populate an object inside an object in an array?

  28. 28

    How to call the Javascript file inside Body tag

  29. 29

    How to call web service inside of forloop?

뜨겁다태그

보관