Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    javascript object question

    I was going through an article on JavaScript objects on "http://www.howtocreate.co.uk/tutorials/javascript/javascriptobject" and there I read

    Intrinsic objects - variable types with constructors.

    * Array
    * Boolean
    * Date
    * Function
    * Image
    * Number
    * Object
    * Option
    * Regular Expression
    * String
    does it mean that, whenever I want to use any of these objects, I have to create it using new and only then I can use its properties or methods ?

    1) There are other objects like document, history, location, navigator, parent and screen. If objects which need to be created using constructor are called intrinsic objects, then what are other objects which can be used without constructor are called as ?

    Array, Boolean, Image, Date etc all start with capital letter. While document, history, navigator etc start with lower case letter. Why is that ? Is there any difference between objects that start with upper case and those start with lower case ? Are they known by different names ?

    Thanks

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,639
    Thanks
    0
    Thanked 649 Times in 639 Posts
    1. If you want your own objects of the types listed then you need to create them usin 'new' - except that boolean, number, string, and functions can be created without it, arrays can be created using [], objects can be created using {} and regular expressions can be created using //.

    history, navigator, window etc are objects belonging to the browser which therefore don't exist if you run Javascript outside of a browser.

    document is the object that provides access to the document object model which is a separate part of JavaScript outside of the core language.

    Image along with Option basically exist for historical reasons as they provided access to create those elements to add into a web page prior to the document object model being created.

    The only difference regarding whether they start with a capital or not is that the capital letter seves as an indicator that you are meant to use it as a template for creating your own objects whereas without the capital you are meant to use it directly. This is merely a convention - there is nothing in JavaScript to require it.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,333
    Thanks
    11
    Thanked 587 Times in 568 Posts
    1) native objects
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #4
    New Coder
    Join Date
    Mar 2012
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts
    history, navigator, window etc are objects belonging to the browser which therefore don't exist if you run Javascript outside of a browser
    I dont understand this. Whatever JavaScript I write is to be read by browser. So all objects belong to browser only. So where is the question of object not belonging to browser ?

    Secondly, Javascript can be run only within browser, it can not be understood otherwise. So how can you run javascript outside browser ?

  • #5
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,278
    Thanks
    12
    Thanked 342 Times in 338 Posts
    Quote Originally Posted by jigneshjsoni View Post
    Secondly, Javascript can be run only within browser, it can not be understood otherwise. So how can you run javascript outside browser ?
    node.js server application, PHP Spidermonkey, JavaScript for PDF and many more
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #6
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    if you have windows, you can execute a .js file. It will open with the Windows Scripting Host (WSH). If you execute this way, you can not call any of the window objects used for the browser, or the DOM. But you can invoke many of the ActiveXObjects from microsoft.
    http://support.microsoft.com/kb/232211
    "Like Microsoft Internet Explorer, WSH serves as a controller of ActiveX scripting engines. WSH has very low memory requirements and is ideal for both interactive and non-interactive scripting needs (such as logon scripting and administrative scripting)."
    Here's an interesting read javascript shell scripting. An article about compiling js into an executable. (which reminds me of HTA's, but even though it's not a browser, HTA's still use the same engine as Explorer, so you do have access to the window object, and the DOM)

    And a necessary resource for all of my non-browser scripting is the Scripting Guys. They answer all kinds of questions about what can be done, and how. Lot's of examples to mod too.
    Allwisend bin ich nicht, doch viel ist mir bewursst
    -Goethe

  • #7
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,639
    Thanks
    0
    Thanked 649 Times in 639 Posts
    There is also Adobe Air which allows you to build complete desktop applications in JavaScript (or two other languages).

    There are so many different places that JavaScript can run that the two places you can attach it where it runs in a browser (either attached to a web page or attached to the browser itself) would get completely overlooked if you had a list of all the different ways to run JavaScript.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #8
    New Coder
    Join Date
    Mar 2012
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, I understand about use of JavaScript outside browsers.

    Still I don't understand comment about DOM
    document is the object that provides access to the document object model which is a separate part of JavaScript outside of the core language.
    Everything that is in JavaScript is part of core language. Then how come document is not part of core language. I believe , this is most widely used object.

    If document is not part core language, then what is included in core language ?

    Is document an instance of DOM ?

  • #9
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,278
    Thanks
    12
    Thanked 342 Times in 338 Posts
    Quote Originally Posted by jigneshjsoni View Post
    If document is not part core language, then what is included in core language ?
    everything mentioned in the JavaScript specification (ECMAScript 262 Edition 5.1)

    Quote Originally Posted by jigneshjsoni View Post
    Is document an instance of DOM ?
    document is an instance of the "Document" interface of the DOM
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #10
    New Coder
    Join Date
    Mar 2012
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for your replies

    I checked JavaScript specification (ECMAScript 262 Edition 5.1), but nothing is mentioned in there about Document Object Model (DOM) api or document or may be I was unable to understand.

    Is dom api a part of JavaScript or completely different from JavaScript ? Does JavaScript use DOM api for accessing elements and both are completely independent from each other ?

    Is dom api part of browser (a browser functionality) or is independent from browser also ? If it's not a part of browser, then is it part of OS ?

    Can you please answer these questions without using any technical jargon or pointing to different links ?

    Thanks

  • #11
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,639
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by jigneshjsoni View Post
    I checked JavaScript specification (ECMAScript 262 Edition 5.1), but nothing is mentioned in there about Document Object Model (DOM) api or document or may be I was unable to understand.

    Is dom api a part of JavaScript or completely different from JavaScript ? Does JavaScript use DOM api for accessing elements and both are completely independent from each other ?
    JavaScript running in a web browser consists of three parts.

    1. The core component defined in ECMAScript 262 Edition 5.1
    2. The Document Object Model (see http://www.w3.org/standards/techs/dom#w3c_all ) JavaScript currently supports DOM 2 and parts of DOM 3.
    3. The Browser Object Model (defined in each individual browser to provide the script with information about the browser - there is no standard for this but in practice all but IE follow the way that the Mozilla web browser introduced).


    So the DOM is a part of JavaScript run in a web browser (and anywhere else that the DOM is applicable) but is not a part of ECMAScript.

    When you run JavaScript outside a web browser (such as on the server or on the desktop) then the BOM part wouldn't apply and a different object would be the global object that almost everything is attached to in place of the window object.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #12
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,333
    Thanks
    11
    Thanked 587 Times in 568 Posts
    Quote Originally Posted by jigneshjsoni View Post
    Is dom api a part of JavaScript or completely different from JavaScript ? Does JavaScript use DOM api for accessing elements and both are completely independent from each other ?
    The dom is separate form javascript, but in browsers, they are 100% correlated with each-other in terms of appearance. All browsers have both the dom and js, some other engines have just js, and old IE can use the dom with VBScript instead of javascript. There is also the TCL scripting language, but i don't thank any browsers support it anymore. Think of the dom as a library for javascript, just like json2.js . that library adds a window.JSON obct that provides methods to javascript, like JSON.parse(), much the same way the the library that adds window.document provides document.getElementsByTagName().

    Quote Originally Posted by jigneshjsoni View Post
    Is dom api part of browser (a browser functionality) or is independent from browser also ? If it's not a part of browser, then is it part of OS ?
    It is up to each browser maker to implement their own dom. All follow a few simple rules (the dom is a spec), but they also like to add their own features, like element.innerHTML, getElementsByClassName(), and others that go above and beyond the DOM specification. It is not tied to the os at all. windows machines have different dom implementations in chrome than firefox than IE.


    Quote Originally Posted by jigneshjsoni View Post
    Can you please answer these questions without using any technical jargon or pointing to different links ?
    not easily, it behooves you to learn the vocab for programming if you are interested in doing so. that's just the breaks.

    if you want to learn more about how the dom works, you can study a dom written in javascript instead of C: https://github.com/tmpvar/jsdom
    but, don't think that that dom is the one that browsers use. I almost heistate to link to it because it might confuse the issue. But, if you peruse the source of jsdom, you'll get a pretty good idea of the dom, what it does, and how it fits into the larger javascript programming picture.
    Last edited by rnd me; 03-11-2012 at 12:55 AM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •