0

Hey guys am new to javascript and when i tried some piece of code its showing type error ..The code is

var something = function(element){
    this.name = "oops";
    this.some = function(element) { console.log(this.name); }; 
    element.addEventListner('click', this.some, false); 
}

When i called the above function like var b = something("baba"); its showing error TypeError: undefined is not a function..I dunno why it happens like this..

Hope you can help me ..Thanks

2
  • 3
    addEventListener is spelled wrong. Commented May 8, 2014 at 17:55
  • will it work if i add like element.addEventListner('click', this.some.bind(this) false); ..here the this points to the object of the function b right ?? Commented May 8, 2014 at 18:04

3 Answers 3

2

First addEventListner should be addEventListener

Next You are binding a click event to the element in question.

Where as you seem to be passing in a string and then binding the event to it. (make sure the argument you pass in is a valid object to which events can be bound to).

The context of this inside the function will be the window object. So you would need to bind the context to the event.

var something = function(element){
    this.name = "oops";
    this.some = function(element) { console.log(this.name); }; 
    element.addEventListener('click', this.some.bind(this), false); 
}

var elem = document.getElementById('elem'); 

var b = something(elem);

Check Fiddle

Sign up to request clarification or add additional context in comments.

3 Comments

will it work if i add like element.addEventListner('click', this.some.bind(this) false); ..here the this points to the object of the function b right ??
+1 for adding complete code plus jsfiddle... you totally beat me :)
this corresponds to the object that we have called right ??..var baby = something() ..so b will act as this right ??
0

Continuing to First addEventListner should be addEventListener

Consider only below thing:

var b = something("baba");

here "baba" is a string. Your function is expecting an element.

eg.

   var something = function (element) {
            this.name = "oops";
            this.some = function (element) { console.log(this.name); };
            element.addEventListener('click', this.some, false);
        }   




<input type="button" value="click here" onclick="something(this);" name="TestName" />     

Above code should work....

1 Comment

this corresponds to the object that we have called right ??..var baby = something() ..so b will act as this right ??
0

First of all, fix the typo mentioned by @Sushanth

Make sure you bind the function to the object you want the some function to have as this.

Here's what the coude should look like:

var something = function(element){
    this.name = "oops";
    this.some = function(element) { console.log(this.name); }; 
    element.addEventListener('click', this.some.bind(this), false); 
}

See it working here: http://jsfiddle.net/y2BPm/1/

Edit: The function you added to this.some could be anything and will have no idea what you meant by using this, unless you actually tell it "bind it" to an object, in which case this inside the function will be whatever you binded it to. In this case, the something function

1 Comment

this corresponds to the object that we have called right ??..var baby = something() ..so b will act as this right ??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.