2

Could someone explain to me how to get namespacing in the QScriptEngine extensions? I've set it up so I have a directory foo/ under script/, with an __init__.js file being executed.

__setupPackage__(__extension__);
print(__extension__);

hello = function() { return 5; };

And in my C++ code: engine.evaluate("hello();"); // works fine

So my question is, what's the point of the file hierarchy like foo/bar/whatever (foo.bar.whatever), if they all get lumped into a global namespace? I've seen some examples, where they try to create a namespace in the code, but I can't seem to get that to work without getting a compiler error.

      foo = {
          hello : function() { return 5; }
      };

and in my C++ code:

      engine.evaluate("foo.hello();");

Am I misunderstanding the way Qt handles namespaces? Should everything indeed be mashed into a global scope regardless of which file it was taken from? Is there a proper example for creating these type of namespaces? Thanks.

voodoogiant
  • 2,118
  • 6
  • 29
  • 49
  • is it possible that QScript does not allow you to create object literals? Try `foo = new Object; foo.hello = function () {};` instead. – Greg Guida Jul 23 '11 at 23:13

3 Answers3

0

Namespaces (as known in languages like C++) do not exist in JavaScript. The only way to get namespace-like behaviour in JS is to encapsulate stuff in an object, in which the object's name defines the namespace

This example will create a 'namespace' foo in JS, containing a method 'bar', and a namespace 'foo.baz' containing a method 'hello':

var foo = {
    bar : function(){ return 5; },
    baz : {
        hello : function(who){ return 'Hello ' + who + '!'; }
    }
};

Hope this helps you out a bit.

Tom Knapen
  • 2,277
  • 16
  • 31
  • Um, it doesn't actually. I understand javascript doesn't have "true" namespaces, but as you can see in my example, I already try putting a function in an object, but I get a parse error, which is my actual problem. Thanks. – voodoogiant Jul 20 '11 at 13:19
  • @voodoo I know you tried, but in your post you forgot the 'var' keyword in front of 'foo'. Is it possible that this is causing the error? – Tom Knapen Jul 20 '11 at 13:32
0

In JavaScript we simulate name-spaces by using closure. This is also usefull for hiding certain functions and properties within your namespace that you dont want to open up to users

(function(window, undefined){
  //declare a local object
  myNamespace = {};

  //define private variables
  var privateVar; // this wont be accessable outside of the closure

  var privatefunct = function () {
    alert('I can only be called by functions defined within the closure');
  }

  myNamespace.publicVar = "this can be accessed outside the namespace";

  // this function can be called outside the closure
  myNamespace.getPrivateVar = function() {
    return privateVar;
  }

  //add your local object to the global object (aka. window in the browser)
  window.myNamespace = myNamespace;
})(window)

This pattern is partially borrowed from jQuery but It has several advantages because it protects your code from mallicious code that might try to change the value of window or undefined.

Im not sure if this totally answers your question but I hope it helps!

Greg Guida
  • 7,302
  • 4
  • 30
  • 40
0

If QScriptEngine is a JavaScript implementation then

var foo = {
          hello : function() { return 5; }
      };

or

var foo = {};
foo.hello = function() { return 5; };

must work just fine. Otherwise provide exact text of syntax error you are getting.

If you do need namespaces you can consider my TIScript: http://www.codeproject.com/KB/recipes/TIScript.aspx

c-smile
  • 26,734
  • 7
  • 59
  • 86
  • The exact error I'm getting is "Parse Error" on the first line. How would I possibly consider getting TIScript working in QScriptEngine if I can get the example above working? – voodoogiant Jul 22 '11 at 13:12