Jump to content

Problem with js class


xenophobia

Recommended Posts

Hi,

I have a simple class here:

function MyClass(id){
    this.input_box = document.getElementById(id);
    this.test_value = "Hello World!";

    this.input_box.onkeydown = function(){
        alert(this.test_value);
    };

    this.test_function = function(){
        alert(this.test_value);
    };
}

usage:
var c_1 = new MyClass("myinputbox");

c_1.test_function();

 

Alright. "myinputbox" is an ordinary textbox. So the class will implement an event when user input a character into the textbox and a msgbox will prompt out.

 

The problem is the msgbox is saying "undefined" instead of "Hello World".

 

But however, if I call it using the test_function, it will prompt "Hello World".

 

Is the problem of variable scope?

Link to comment
Share on other sites

The problem is that you create a closure when you apply a function to an event. Closure functions have their own scope, or their own "this," understand? So the this inside of your onkeydown refers to the element itself, not the MyClass function.

 

To get around this, you need to use apply to bind the keyword this to the onkeydown function of the MyClass function. Check it out

 

        <script type="text/javascript">
            Function.prototype.bind = function(object) {
                var method = this;
                return function() {
                    method.apply(object, arguments);
                }
            }
            
            function MyClass(id){
                this.input_box = document.getElementById(id);
                this.test_value = "Hello World!";
            
                this.input_box.onkeydown = function(){
                    console.log(this.test_value);
                }.bind(this);
            
                this.test_function = function(){
                    alert(this.test_value);
                };
            }
            onload = function(){
                new MyClass('test');
            }
        </script>

 

Function.prototype.bind adds the bind() method to any function allowing you to change the scope of "this"

 

now to help you understand what I am saying, in your onkeydown function remove the bind call and alert(this.value); this is the object or the input field

 

this.input_box.onkeydown = function(){
                    console.log(this.value);
                };

Link to comment
Share on other sites

If you are having trouble calling a variable outside of the function then your variable scope is local not global. If you want to make the variable scope global (that way you can call a variable outside of a function) then you create the variable OUTSIDE and BEFORE the original function is created.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.