...

View Full Version : javascript object question



jigneshjsoni
03-09-2012, 12:45 AM
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

felgall
03-09-2012, 02:13 AM
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.

rnd me
03-09-2012, 02:40 AM
1) native objects

jigneshjsoni
03-09-2012, 07:49 AM
history, navigator, window etc are objects belonging to the browser which therefore don't exist if you run Javascript outside of a browserI 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 ?

Dormilich
03-09-2012, 03:33 PM
Secondly, Javascript can be run only within browser, it can not be understood otherwise. So how can you run javascript outside browser ?

node.js (http://nodejs.org/) server application, PHP Spidermonkey (http://pecl.php.net/package/spidermonkey), JavaScript for PDF (http://www.adobe.com/devnet/acrobat/javascript.html) and many more (http://en.wikipedia.org/wiki/Javascript#Uses_outside_web_pages)

blaze4218
03-09-2012, 04:08 PM
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 (http://www.phpied.com/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 (http://technet.microsoft.com/en-us/scriptcenter/dd901334). They answer all kinds of questions about what can be done, and how. Lot's of examples to mod too.

felgall
03-09-2012, 08:14 PM
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.

jigneshjsoni
03-10-2012, 11:04 AM
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 ?

Dormilich
03-10-2012, 01:19 PM
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 (http://www.ecma-international.org/publications/standards/Ecma-262.htm))


Is document an instance of DOM ?
document is an instance of the "Document" interface of the DOM

jigneshjsoni
03-10-2012, 06:59 PM
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

felgall
03-10-2012, 11:57 PM
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.

rnd me
03-11-2012, 01:52 AM
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().



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.




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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum