last day (17 days later) » 

9:50 PM
2
A: javascript functions and objects using keyword 'this' does not work

bfavaretto1. Functions are constructors for new objects When you call new FuncName, the function acts as a constructor, and the value of this inside points to the object being constructed (not the function itself). When you remove new, this becomes undefined, falling back to the global object (unless you'...

 
Thanks bfavaretto. now I understand that 'this' points to object being created. can you explain what you mean by "when you remove new, 'this' becomes undefined..." and regarding object.create and new, does it mean everytime an object is constructed using new, its prototype needs to be initialized, can I not have it part of the new construction itself by placing function methods inside the main function itself.? Thanks again
 
When you call a function without new, it's not invoked as a constructor, and this is not set to the object being constructed (as there is none). The following code will create a global variable named bar: function foo(){this.bar = true}; foo();, because this will mean the global object (window).
The difference between using Object.create or new has more to do with inheritance. Object.create just allows you to setup inheritance in a less verbose way (as long as you don't need a constructor).
 
+2:Thanks again for the clarification. in this example,var obj = function() {
this.x= 1;
return {
increment: function() {
this.x = this.x + 1; }
}
};

var test1=new obj();
test1.x //undefined ...but when I remove the return statement section and just have var obj = function() {
this.x= 1; }; var test1=new obj(); test1.x//1...why having the return statement does not allow access to x..
 
@user1988876 Because in this case the constructor is not returning the object it was constructing, but the new one you told it to return; this inside the constructor refers to the former.
 
following up on what you said, I made one line change here;var obj = function() {
this.x= 1;
return {
increment: function() {
this.x = this.x + 1;
return this.x;}
}
};

test1 = new obj();
var c=test1.increment();
c;//now the value of c is NaN..I dont understand why it is NaN
 
9:51 PM
It's for the same reason I explained in my previous comment: you are returning a different object, which doesn't have a x property initialized to 1 when it's constructed. So this.x is undefined, and undefined + 1 is NaN.
Try this:

var obj = function() {
this.x= 1;
return {
x : 2,
increment: function() {
this.x = this.x + 1;
return this.x;
}
}
};
var o = new obj(); // you don't even need new here
o.increment(); // 3
 
in your solution, the initalization this.x=1 is not necessary rite
and why do you see you dont need new here...
 
this.x=1 refers to an object that's being thrown away, as you opted to manually return a different object. And new is only required to set that unnecessary this to the wasted object. The this inside increment refers to the object where that method belongs.
Basic rule is: if you return {...} from a function, it's unlikely you'll need to call it with new.
this can be really confusing in js... it took me a while to really understand it.
 
I have been spending reading Crockford's book and I get stuck with one or two pages a day and that too not with clear understanding. here is another example. in the above, I created a function and assigned to obj. now I create a object and assign it to obj
var obj = {
x: 1,
increment: function() {
this.x = this.x + 1;}
};

obj.increment();
obj.x//2
obj.x=4;
obj.increment();
obj.x//5
what I dont understand here, is I dont return anything, but it still I could access obj.x but the x value of obj is not getting updated. I mean in the first call after obj.increment(), obj.x is 2, I reassign obj.x=4 and then increment it, now I expect the value to be 6 (since x is previously stored as 2). but it does not
 
When you reassign, you are replacing 2 with 4. Then you increment, 4 + 1 = 5
 
10:08 PM
yeaaa..Thanks, that stupid of me. One other questions is I dont have return statement here, I could still get access to x value. while in the previous example, I needed return to access x value. why is the difference?
 
no difference, considering your example. But if you return, you can do var someNum = obj.increment() to get the value of x. Without the return, if you do that someNum will be undefined
 
10:27 PM
looking at both kind of examples above, I summarize them here.
Example 1: why no 'new' required here..
var obj = function()
{ this.x= 1;
return { x:1, increment: function() {
this.x = this.x + 1;}
}
};
test1 = new obj();
test1.increment();
test1.x;
example 2:
var obj = { x: 1,
increment: function() {
this.x = this.x + 1;} };
obj.increment();
obj.x
both example 1 and 2 does same thing, in example 1 i create a function and in example 2 I create an object. is any one superior than the other?
 
10:52 PM
In example 1 you create a function that returns and object. In example 2 you create the object directly. Example 1 is only useful if you'll be using the function arguments inside the object, or want a "private" property (via closure)
But let's not complicate things
Usually I'd suggest: use what looks simpler and is easier to understand, unless it fails to meet your requirements.
 
Thank you so much for your time. I will be starting on prototype and closures next. If I have some questions, can I ask you?
 
If you have further questions, post them on Stack Overflow. If I'm not there, someone else will probably help you
 

  last day (17 days later) »