top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How this works in JavaScript

+4 votes
121 views
var fullname = 'John Doe';
var obj = {
   fullname: 'Colin Ihrig',
   prop: {
      fullname: 'Aurelio De Rosa',
      getFullname: function() {
         return this.fullname;
      }
   }
};

console.log(obj.prop.getFullname());

var test = obj.prop.getFullname;

console.log(test());

how does this code work in javascript?

posted Dec 30, 2014 by Merry

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+3 votes
 
Best answer

The output of the first logging is Aurelio De Rosa and second is John Doe.
This is actually a flaw in the javascript. "this" is a reference to the current object instantiated by the class. In JavaScript, ‘this’ normally refers to the object which ‘owns’ the method, but it depends on how a function is called
When the First time obj.prop.getFullname() is logged the "this" refers to an instance of the obj but the second time when test() is logged the "this" refers to the global object. In web browser, that’s ‘window’.

Thus it's pretty clear why the o/t is such. First time this.fullname is local fullname i.e "Aurelio De Rosa" but second time this.fullname is global fullname i.e "John Doe"

answer Dec 30, 2014 by Prakash
Similar Questions
+1 vote

What i am trying to achieve is creating some objects by following a better pattern. I am using Raphael.js library for creation of graphical shapes.

Problem is at the line:

X: "" + this.canvas_X + this.paper["width"]/2 - self.width/4,

The error i am getting is :

Uncaught TypeError: Cannot read property 'width' of undefined

I figured out that error is at this.paper["width"]/2. I am new to using javascript and not able to understand what error i am doing

var Graphics={
        create_canvas: function(){
                $("#canvasdiv").append("<div id='id1' width='80px' height='50px'></div>");
        },
        canvas_X:get_LEFT_TOP("canvasdiv")[0],
        canvas_Y:get_LEFT_TOP("canvasdiv")[1],
        canvas_width:function(){return $("#canvasdiv").width()},
        canvas_height:function(){return $("#canvasdiv").height()},
        paper:{
            width:900,
            height:700,
            create: function(){
                return new Raphael(document.getElementById('canvasdiv'),this.width,this.height);
            }
        },
        vswitch:{
            color:"Black",
            text:"vSwitch",
            width:"150",
            height:"150",
            X: "" + this.canvas_X + this.paper["width"]/2 - self.width/4,
            Y: Graphics.canvas_Y,
            delay:"2000",
            create: function(paper){
                setTimeout(function(){
                        paper.rect(X,Y,width,height).attr({fill : "black"})
                        },delay);
            }
        },
}

This is the way the call is being made:

 Graphics.create_canvas();
    p=Graphics.paper.create();
    Graphics.vswitch.create(p);
0 votes

I need to reuse my Javascript module in typescript for my angular application. Can you guide me how to do that ?

...