1

I was reading SpiderMonkey tutorial of how to embed and execute javascript code in c++ program via JSAPI spidermoneky. but I didn't totally understand what does the global objects do and what is the role of both JS_class and JS_NewGlobal? also what does it mean by customizing your global objects?

sara
  • 256
  • 1
  • 3
  • 15
  • Please provide a link to the doc that you're reading. – cdleary Dec 02 '13 at 01:03
  • Sorry, here is the document https://developer.mozilla.org/en-US/docs/SpiderMonkey/JSAPI_User_Guide – sara Dec 02 '13 at 14:06
  • [SpiderMonkey - JSAPI User Guide](https://web.archive.org/web/20210528173125/https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/JSAPI_User_Guide) document archived by Wayback Machine (archive.org) – Erman Kadir Kahraman May 23 '22 at 04:15

1 Answers1

0

In JavaScript you have a top level namespace which actually has properties similar to normal javascript objects, so it is called the "global object". What you normally think of as global variables in a language like C are generally properties of the global object. Top level functions also live there, which is why you see bindings for top-level things like srand and system being created for the global object in that document. You might want to put different functions in different global environments. Embedders like to do all kinds of customization things.

JS_Class is a SpiderMonkey concept for a native type that backs objects in the JS runtime. When you want to make a new, special builtin type you do so by defining a JS_Class, and then objects instantiated from the class can have their class tested in native code to see if it matches. HTH!

cdleary
  • 69,512
  • 53
  • 163
  • 191
  • you wrote "In JavaScript you have a top level namespace which actually has properties similar to normal javascript objects," is there a difference between JavaScript and javascript, or you mean spidermonkey.Regarding the global object, what I understood till this moment, is that it includes the top level function that are used by the embedded javascript code, that will be passed to c++ spidermonkey code, so for example if embedded java script code needs foo function it will call the foo in the global object, is this right ? and if I want to write codes and use spidermonkey, what should i do ? – sara Dec 18 '13 at 21:29