I am trying to wrap my head around the behavior of referenced errors created in JavaScript.
In the following example, a is ReferenceErrorthrown into the second line, and execution is aborted:
ReferenceError
var obj = {}; obj.func1 = func2; alert('Completed');
In this example, the code succeeds, although it obj.func1remains undefined:
obj.func1
undefined
var obj = {}; obj.func1 = func2; var func2 = function() { alert('func2'); }; alert('Completed');
My guess was that the second line would have the same error, and when it wasn’t, Id expected it obj.func1to refer to correctly func2, but Ive been double blind. So what exactly is going on here?
func2
var ; . , :
var
var obj; var func2; obj = {}; obj.func1 = func2; func2 = function() { alert('func2'); } alert('Completed');
, , func2 , undefined. , ReferenceError.
Javascript "hoisting".
, var, , Reference Error. , , , . , :
Reference Error
var func2; var obj = {}; obj.func1 = func2; func2 = function() { alert('func2'); }; alert('Completed');
, , obj.func1. , undefined obj.func1. func2 .
Your func2 variable is not displayed. This is why obj.func1 remains undefined.
var obj = {}; var func2 = function() { alert('func2'); return "Test"; }; obj.func1 = func2; alert('Completed');
Source: https://habr.com/ru/post/1629306/More articles:How to access Webpack module from dev browser tools? - javascriptDatatables standalone data editor in Java Servlet - javaIs there a Javascript TCP Soket Library for PHP like SignalR with .NET? - phpChecking Yii2 Image Size - phpPHP date_format (): how to format a date from a string value - phpMongodb aggregates $ push with $ cond and $ each - node.jshow to convert json incoming date to java date format? - javaDifference between `load data inpath` and` location` in the hive? - sqlWhen haskell pivot points need to work with extra “space”? - parsingAmazon MWS API (Order Update and Real-Time Updates) - apiAll Articles