-2

In Javascript, like setInterval(draw,20) you don't include anything like in C#. You can just use the function. The function is on the Windows object of the browser.

  1. Can the following objects and its functions be called without any pre-definitions?

    Window
    Navigator
    Screen
    History
    Location
    
  2. Does JavaScript has any built-in functions?

5
  • 3
    The first question isn't clear. The second makes no sense. Commented Sep 9, 2012 at 23:06
  • 1
    C# and Javascript look mildly similar, but are not similar at all in practice. Sorry. Commented Sep 9, 2012 at 23:07
  • I've added the C# tag in case someone with both C# and Javascript skills can help here. Commented Sep 9, 2012 at 23:08
  • 1
    stackoverflow.com/questions/8693965/… Commented Sep 9, 2012 at 23:08
  • 1
    What objects are available depend on what is provided by what is hosting your JavaScript. While in a browser you can depend on the Window object, in another host such as Node.js, the Window won't exist. Built-in functions and what not also depend on what version of JavaScript/ECMAScript. Commented Sep 9, 2012 at 23:09

2 Answers 2

3

In Javscript, there are certain things which are part of the language and will also be there. Native objects, like Array(), String() and Number(). (Not a complete list). These need no prefix in front of them, because there are none.

In addition, when running on the browser the host environment provides a global variable called window that is the same as the anonymous Javascript global that everything runs in. Methods of the window object include things like alert() and setTimeout()

Alert can be called with window.alert() or more often by just alert() itself. (Some people consider calling window.alert() best practice to a) show you are calling the global function and b) protect you from any local variables named alert.)

Clarification:

The Javascript engine keeps a global variable with no name and not normally accessible. When not running in struct mode, undeclared variables are created as properties in this global object, all global functions are put in the global object and this normally called functions points to the global object.

When running in the browser, window is the global object.

Sign up to request clarification or add additional context in comments.

15 Comments

the host environment provides a global variable called window that is the same as the anonymous Javascript global that everything runs in - I don't know why, but this bothers me.
@JaredFarrish -- I couldn't figure out a better way to phrase it... but I'll try.
Maybe that's what's the problem. If you have no scope, you're in window. See: jsfiddle.net/3dy8S window is default to a more specific scope (in browser clients). The thing is, there's a base scope, and window in browsers is that scope.
@JaredFarrish "If you have no scope you are in the window" is only true if you are running Javascript in the browser. If you are running it anywhere else, you are in the Javascript unnamed global object.
Yes, that's why I said "in browser clients". I have no node.js experience, or other (?) library executables. In a browser, it's window.
|
0

In short, yes to both :)

Assuming you're interested in client side javascript (running on a webpage within a browser), I strongly suggest you follow the tutorials on code academy, which will answer your questions by teaching you how Javascript works, how you interact with the browser, and what the functions and methods the browser makes available to you.

Once you've finished the tutorial, things should be a lot clearer, and since you already know C# it shouldn't take too long to adjust.

6 Comments

Oh lord, please refrain from w3schools links on SO, see w3fools.com.
Good gravy, I had no idea. It's been many, many years since I read the w3schools tutorial. Edited to point at code academy instead
Well, I get an error when I try that link.
Ok, that link works now. :)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.