I have the following code in a non-browser environment (Titanium Studio) and I noticed that both result1 and result2 work, return the same result, and are the same type (object).
Could you explain if they are different in any and how they are different?
In my case inside name1 there is no reference to the local context (no ‘this’ in there, just local variables) and I am trying to figure out if I should be writing all my code with or without ‘new’ when I have a situation like the one below.
var name1 = function(some_arg){
// some stuff
return result; // returns an object
}
var result1 = name1('some_value');
var result2 = new name1('some_value');
typeof(result1); // returns object
typeof(result2); // returns object
Thanks!
Edit Below: The original question was answered and I am now looking for a clarification on something related.
Code related to comment below:
function Name1 (name){
this.name = name;
}
var version1 = new Name1('joey');
version1.name;
var Name2 = function(name){
this.name = name
}
var version2 = new Name2('joey');
version2.name;
Thanks!
thisis bound to a new object when usingnewwhile it is bound to the global object when not usingnew. If you do not usethisin the constructor function or any methods you add to your object, it indeed does not really matter – however, you should use it anyway or you’ll have a problem if you ever do use it.