문제

First time poster. I tried to search for answer before posting.

I'm new to javascript, coming from a Delphi/pascal background. I am used to things having to be explicitly spelled out. When I see code that includes setTimeout or setInterval (see here: http://www.w3schools.com/js/tryit.asp?filename=tryjs_setinterval1), I notice that it just looks like an intrinsic function. But I think it is actually a method of the Windows object.

So it looks like the "window." part is optional. Is this somethign specific to the Window object? Or will any method that is specified without the name of it's containing object be recognized in javascript? And what would happen if there was another variable in the global scope that had a method also named setTimeout? How would the compiler/interpreter know which one to use?

Thanks so much,

Kevin

도움이 되었습니까?

해결책

In Javascript, there is something called the Global scope. On Browsers, the global scope object is window. If you create a variable outside of a function, it goes by default into the global scope, which is window.

When invoking a method or referencing a variable on the window object, it's optional to include window.

다른 팁

its not windows its window (no 's' at end). As far as i know javascript have 3 scopes local, [clouser], global

Every variable or function is first looked up in local scope, then clouser and finally if not found anywhere in between it will checked in global scope.

As in image you will see global scope is shown as Window object which mean whatever goes in global scope goes in window object.

copy paste below code in chrome devtools console

function outerFunction() { 
   var a = 10; 
   debugger; 
   function innerFunction() {
      'use strict'; 
      var b = 10;
      debugger; 
      console.log(a);
   } 
   innerFunction();
}
outerFunction();

on 1st debug on 1st debug

on 2nd debug on 2nd debug

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top