I am using Meteor JS.
I have a JavaScript function defined in file A which I want to reuse by calling from file B. Example:
File A:
function Storeclass(){}
Storeclass.validate=function(){...}
From A JavaScript I try to call StoreClass.validateBasic() it works but the same call doesn't work from B. Also I tried in B doing var storeClassObj=new StoreClass(); and storeClassObj.validate(). I get error ReferenceError: StoreClass is not defined.
Read this doc about namespacing in Meteor.
The relevant portion is this:
// File Scope. This variable will be visible only inside this
// one file. Other files in this app or package won't see it.
var alicePerson = {name: "alice"};
// Package Scope. This variable is visible to every file inside
// of this package or app. The difference is that 'var' is
// omitted.
bobPerson = {name: "bob"};
However, later on in the same doc, it says this:
When declaring functions, keep in mind that function x () {} is just shorthand for var x = function x () {} in JavaScript.
This suggests that the function you have written is private to the file A and cannot be accessed from file B, even if load order is correct!
Because your function in file B might invoke before File A is ready so you have to make sure that all required js files are loaded successfully.
If you are using jQuery then in file B call your function in document ready function:
$( document ).ready(function() {
//File A code
});
or in plain JavaScript:
(function() {
// your page initialization code here
// file A code
})();
Related
I have a couple of JavaScript scripts to house my functions (for modularity and reuse). I load them both from the windbg script I'm running. From within one script, how do I call a function defined in the other?
This engine doesn't seem to support the import/export feature employed by browsers.
From within the debugger script, I have to use #$scriptContents to access JavaScript functions.
How do I accomplish something similar from within one of the JavaScript functions?
Experiment
I was hoping there would be some sort of global namespace for all JavaScript functions, but it appears not.
Consider
// t1.js
function func1() {
host.diagnostics.debugLog('func1()...\n');
}
and
// t2.js
function func2() {
host.diagnostics.debugLog('func2()...\n');
func1();
}
In my cdb session
0:000> .load jsprovider.dll
0:000> .scriptload t1.js
JavaScript script successfully loaded from 't1.js'
0:000> .scriptload t2.js
JavaScript script successfully loaded from 't2.js'
0:000> dx #$scriptContents.func1()
func1()...
#$scriptContents.func1()
0:000> dx #$scriptContents.func2()
func2()...
Error: 'func1' is not defined [at t2 (line 3 col 5)]
Edit
Per #Mosè Raguzzini's comment and this answer, I went looking for some way to reference "foreign" functions.
I eventually unearthed this
host.namespace.Debugger.State.DebuggerVariables.scriptContents
as a container for all functions. Is this documented somewhere? Is there no simpler way to get there? (I realize I can just assign a short variable to that object; I'm just suspicious this this is more of a backdoor into something with a very simple front door, but I don't know where the front door is.)
AFAIK all scripts are imported in global scope, so you can act as them are written in a single file, once all are loaded.
Example (REF to blabb answer)
common.js has a few functions that are normally reusable like
host.diagnostics.debugLog()
First load it using .scriptload
Then in other js files create a var to those functions and use it
contents of common function file
C:\>cat c:\wdscr\common.js
function log(instr) {
host.diagnostics.debugLog(instr + "\n");
}
function exec (cmdstr){
return host.namespace.Debugger.Utility.Control.ExecuteCommand(cmdstr);
}
a js file using the function from common.js
C:\>cat c:\wdscr\usecommon.js
function foo(){
var commonlog = host.namespace.Debugger.State.Scripts.common.Contents.log
var commonexec = host.namespace.Debugger.State.Scripts.common.Contents.exec
commonlog("we are using the logging function from the common.js file")
var blah = commonexec("lma #$exentry")
for(var a of blah) {
commonlog(a)
}
}
actual usage
C:\>cdb calc
Microsoft (R) Windows Debugger Version 10.0.16299.15 X86
0:000> .load jsprovider
0:000> .scriptload c:\wdscr\common.js
JavaScript script successfully loaded from 'c:\wdscr\common.js'
0:000> .scriptload c:\wdscr\usecommon.js
JavaScript script successfully loaded from 'c:\wdscr\usecommon.js'
0:000> dx #$scriptContents.foo()
we are using the logging function from the common.js file
start end module name
00f10000 00fd0000 calc (deferred)
#$scriptContents.foo()
0:000>
well you can write a javascript function that calls any function from any script sitting in any directory
using something like below (you may need to tweak it the POC worked on my machine for a .js that returned a string)
function runFuncFromAnyScript(dir,script,somefunc) {
var unl = ".scriptunload " + script
host.namespace.Debugger.Utility.Control.ExecuteCommand(unl)
var pre = ".scriptload "
var post = "dx #$scriptContents." + somefunc
var cmd = pre + dir + script
host.namespace.Debugger.Utility.Control.ExecuteCommand(cmd)
return host.namespace.Debugger.Utility.Control.ExecuteCommand(post)
}
used like
0:000> dx #$scriptContents.runFuncFromAnyScript("f:\\zzzz\\wdscript\\","mojo.js","hola_mojo(\"executethis\")" )
#$scriptContents.runFuncFromAnyScript("f:\\zzzz\\wdscript\\","mojo.js","hola_mojo(\"executethis\")" )
[0x0] : hola mojo this is javascript
[0x1] : hello mojo this is the argument you sent to me for execution I have executed your executethis
[0x2] : #$scriptContents.hola_mojo("executethis")
I am using PhantomJS for page automation. For my script, I need to load another script which is also used on the actual client-side of my webpage. This script contains some parts where global variables - which are assumed to be set before the script is loaded - are used.
Now my problem is that I can't figure out how to set these variables before I require them within PhantomJS.
This (obviously) didn't do the trick:
variableX = 1024;
var moduleX = require('myScripts.js');
Now what's the proper and intended way ( if there is one) to do this?
If you prepare the testing script as a CommonJS module, you can require it and use its methods and variables in PhantomJS.
From the docs: http://phantomjs.org/release-1.7.html
As an example, supposed there is a script universe.js which contains the following code:
exports.answer = 42;
exports.start = function () {
console.log('Starting the universe....');
}
This module can be used in another script like the following:
var universe = require('./universe');
universe.start();
console.log('The answer is', universe.answer);
And if you want to assign global variables from such a module you can use the global object as in node.js:
exports.start = function () {
global.success = true;
console.log('Starting the universe....');
}
I have a function like this:
function oneFunction()
{
var onevaribale = $('#onevalue').val();
var twovariable = $('#twovalue').val();
//Do something with both variables
//continue using **onevariable** not noticing the typo as **onevaribale**
}
Now because of the typo, the entire function fails and the location of where the code is run is on an intranet. Luckily, the code has an external js file and I want to assign something like:
var onevariable = onevaribale
How do I go about extending oneFunction to make this change from the external js file?
I am using MVC3 with heavy usage of ajax to get Partial Views. If Partial view contains JavaScript then it is added as a new js file as shown in snapshot:
so If I have a js function:
function checkValue(){
//do work
}
on ajax call a new dynamic JS file will be added contained this function and it conflicts with old once.
myfile.js contained:
function checkValue(){
//do work
}
and 1.js (dynamic file) will contain it too
function checkValue(){
//do work
}
So when I call it due to presence in old file it call already present function which is outdated. How to solve this situation like new JavaScript replace old one.
Thanks
You can check whether something is defined and redefine it only if it is not:
var checkValue = checkValue || function () {
//do work
};
If you want your definitions to override each-other instead of defining the function with a name, define them on the global object each time:
window.checkValue = function () {
//do work
};
I'm testing a js function that uses functions from other js files.
One of my external js files has a function defined as such:
functionname.functionextension = function () {.....}
when testing using jasmine, and calling functionname.functionextension, it complains that functionname is not defined. I think it believes that functionname is an object..
I know that one way to get around this is to modify the function name but I can't do that. Is there any other way?
Thanks
In javascript, all functions are objects. In the external js file, the function is probably defined like this:
var functionname = functionname || {};
functionname.functionextension = function () {
...
};
If you're getting a script error that functionname is not defined, there is either an error in the external javascript or you are not calling some initialization function that the external script requires to set up its objects.
It worked for me...u need to call function by its full name like functionname.functionextension() while calling.