This is done to get the correct result, and the Chrome debugger says there were no exceptions:
var x = new Foo().bar().baz();
but it goes into space and never ends, and the Chrome debugger says that although bar () is executed correctly, it then throws a "typeerror" exception and never goes to the specified function when trying to call baz ():
var x = new Foo(); x = x.bar().baz();
It seems to me that they are functionally identical. Why do they behave differently?
My own question was answered - and boy, I feel stoopid! I'm sorry. A short answer to the question "Why do they behave differently?" - they are not .
, , , ... ! , "baz()", , ell ( "l" ) .
, . ( , - .)
, , . Chrome "typeerror", Firebug " ", .
: : -)
, Chrome:
function Foo(){} Foo.prototype.bar = function() { return this; } Foo.prototype.baz = function() { return 'baz'; } var x = new Foo().bar().baz(); console.log(x); // baz
(Firefox, IE, Chrome).
:
var x = new Foo(); x = x.bar().baz() console.log(x); // baz
.
You need brackets:
var x = (new Foo()).bar().baz();
Otherwise, the statement newapplies to the return value Foo().bar().baz(), all of which will be executed before an attempt is made to create an instance.
new
Foo().bar().baz()
Update . I was informed that this, as they say, is wrong. But I think the comments are interesting, so I am not deleting the answer.