question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to make a function global?

See original GitHub issue

The question title is a bit weird, but I’m going to explain it anyway 😃

Normally, in NodeJS or in a browser, when you store a variable or function on the global or window object it will be globally available in all scripts (without using window. or global. qualifiers), right? How can this be done in duktape? I already created a global object, but don’t know how to continue.

Maybe I’m misunderstanding something here, so let me quickly explain what I’m actually trying to do: my app can now load the Jasmine test framework node module, which defines some functions like describe() or it(), just like Mocha does. You can usually just call them without a need to “require” something or have an object to call things on. However, in my test spec files I get an error that describe is unknown. So I wonder what could be wrong here? The frameworks seems to load fine (no errors and I get output from it).

The call stack is:

Could not load main script: main.js
ReferenceError: identifier 'describe' undefined
    at main (<path>/tests/self-tests/path-test.js:4)
    at require () native 
    at forEach () native 
    at runtTests (main.js:26)
    at global (main.js:35)
    at [anon] (node_modules/jasmine/lib/jasmine.js:198)
    at [anon] (eval:1)

and path-test.js contains:


"use strict"

describe("Blah", function() {
  it("Do it", function() {
    print("testing");
  });
});

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:15 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
svaaralacommented, Jun 16, 2017

I’m assuming you want to do this from Ecmascript (not C) code.

If a script just writes:

foo = 123;

it will establish a global “foo” property (assuming no shadowing variable exists) from either program code, eval code, or even function code (which includes modules). If you say:

var foo = 123;

a global variable is established if the statement is executed as program code or eval code in the global context. In a function it establishes a local variable.

You can gain access to the global object from any scope as follows:

var global = new Function('return this;')();
global.myProperty = 123;

That looks a bit weird, but what it does is: (1) create a non-strict function that returns its “this” binding, and (2) call the function. The call uses “undefined” for the “this” binding, but because the function is not strict, it gets coerced to the global object (which is standard ES “this” binding handling) for the call. The return value is thus always the global object, regardless of where the idiom occurs.

0reactions
fatcerberuscommented, Jun 18, 2017

In other words, if one does this:

var a = {};
var b = a;
b.foo = 812;
console.log(a.foo);

That should print 812. The global reference is no different.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Global functions in javascript - Stack Overflow
@Jordumus that's not correct at all. Open JS console, write var x = function() {console.log('x')} and then try to call window.
Read more >
Python Global Keyword (With Examples) - Programiz
In Python, the global keyword allows us to modify the variable outside of the current scope. It is used to create a global...
Read more >
Global functions in multi-file projects in C - YouTube
Global functions in multi-file projects in C. 4K views 11 months ago ... How to create threads in a loop (pthread_create). CodeVault.
Read more >
How to Use Global Variables Inside a Python Function?
Article Link: https://blog.finxter.com/how-to-use- global -variables-in-a-python- function / ...
Read more >
Declare variables as global - MATLAB global - MathWorks
Share Global Variable Between Functions. Create a function in your current working folder that sets the value of a global variable.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found