calling a function from its string name on Window object - javascript

Why is the test fuction declaration not found in the window object? Thanks
!function(){
function test(){
console.log("testing");
}
var check = window["test"]
console.log(check); //undefined
}();

Since function test() is local to the scope of the toplevel function expression, it's not bound to window, the global scope. You can refer to it as a local variable:
!function() {
function test() {
console.log('testing')
}
console.log(test)
}()
Or bind it directly to window for a global variable:
!function() {
window.test = function test() {
console.log('testing')
}
var check = window['test']
console.log(check)
}()
You cannot access the local scope as a variable - see this question for more details.

Related

Why the result is 'scope is not defined'

I want to know why the result is scope is not defined
function checkscope() {
var scope = "local scope";
function f() {
console.log(scope);
scope = '123' //为什么这里不是全局变量scope,最后结果为什么是scope is not defined
}
return f;
}
var foo = checkscope();
console.log(foo());
console.log(scope)// 结果是scope is not defined
Because you have defined it out of scope i.e. functional scope.
Please define it out of the function scope. So whenever you call your function. The scope value will be changed.
var scope;
function checkscope() {
scope = "local scope";
function f() {
console.log(scope);
scope = '123' //为什么这里不是全局变量scope,最后结果为什么是scope is not defined
}
return f;
}
var foo = checkscope();
console.log(foo());
console.log(scope)/
scope is declared in the function checkscope. It ceases to exist with the } ending that function.
Thus it no longer exists at the point you use it.

Define global function inside anonymous closure function

Is there any way to define within closure a global function? I would like to define one global function so I can call it with onclick event.
(function(){
var private = 'private msg';
function myprivate() {
alert(private)
}
function wanttobeglobal() {
alert(global)
}
})();
Is there any way?
Very bad practice, but the only way short of declaring the variable outside the initial closure is to implicitly create a global variable.
wanttobeglobal = function() {
alert(global);
};
The better way, if you can modify code outside of the closure, is with var:
var wanttobeglobal;
(function() {
// ...
wanttobeglobal = function() {
alert(global);
};
})();
Assign the function to window:
(function(){
var private = 'private msg';
function myprivate() {
alert(private)
}
window.wanttobeglobal = function wanttobeglobal() {
alert(global)
}
})();

Javascript Variable Declaration: this versus var

Is there a difference between
function test(){
var myVar;
}
and
function test(){
this.myVar;
}
The value of this is determined by how function is called whereas var VARIABLE_NAME will create a variable in the local-scope of the function.
In second example, you are creating Object-Constructor using which you can create many instances of the test object using new operator
function test(name) {
this.name = name;
}
console.log(new test('Abc'));
console.log(new test('Xyz'));
There are two scopes in javascript , local or function scope and global scope .
In your case this is global and var is local scope/function scope .
If you use this inside IIFE (immediate invoking function expression)and 'use strict' its not global
The most difference is that your first code works like a private property (local scope) and the second is like a public property (global scope).
Examples:
var test = function(){
this.myVar = "test var";
}
var test2 = function() {
var myVar = "test2 var";
}
alert((new test()).myVar);
alert((new test2()).myVar);

jquery nested function access

How can I access the test2() function outer either in onclick , as shown in the plain function, wrapped in parenthesis to tell the runtime to return the function to the parent scope, once it's returned the function is executed using line 4, maybe reading through these steps will help.
<p onclick='test().test2()'> some text </p>;
<script>
//the below declaration will not change
var jQuery = 'Hi';
(function ($) {
function test(){
function test2(){
alert('text')
}
alert($)
}
console.log($);
test().test2()
})(jQuery)
</script>
You can only access test2 if you declare the function in the same scope as where you want the to access the function. In this case it means you need to declare the function in the outer scope.
You can do so by setting a variable in the outer scope like this:
var test2;
var jQuery = 'Hi';
(function ($) {
function test(){
test2 = function(){
alert('text')
}
alert($)
}
console.log($);
test(); //test needs to be called first to define test2.
test2();
})(jQuery)
I checked twice and this works:
var jQuery = 'Hi';
window.test = (function ($) {
function test(){
this.test2 = function () {
alert('text')
};
alert($);
return this;
}
console.log($);
test().test2();
return test;
})(jQuery);

win1 = window.open and win1.close() in different functions not working

I have 2 separate functions in a javascript.
Function one opens a window with
win1 = window.open (....);
Function two closes the window:
win1.close();
If those actions are in one function it is working but not in the separate functions as above. It somehow looses the object win1 from one function to another.
Any help would be very welcome!
You need to declare the win1 variable outside both functions so that they're in the variable scope of both.
var win1; // This variable is available in the variable scope of any
// ...functions nested in the current scope.
function f1() { // This function has access to its outer variable scope
win1 = window.open(); // ...so it can access the "win1" variable.
var foo = 'bar'; // This variable is not available in the outer scope
} // ...because it was declared inside this function.
function f2() { // This function has access to its outer variable scope
win1.close(); // ...so it can access the "win1" variable.
var bar = 'baz'; // This variable is not available in the outer scope
// ...because it was declared inside this function.
alert(foo); // Gives a ReferenceError, because "foo" is neither in the
// ...current, nor the outer variable scope.
}
f1(); // Invoke f1, opening the window.
f2(); // Invoke f2, closing the window.
alert(foo); // Would give a ReferenceError, because "foo" is in a nested scope.
You can also define win1 globally:
window.win1 = window.open(...);
You could also declare a global variable. You can then just use it anywhere in your scripts.
var win = window.open("URL"); // or window.win = window
win.open();
win.close();
You have to pass win1 as a variable to both functions or have it outside those 2 functions.
function somethingHappened() {
var url = "http://....";
var win = window.open(url);
one(win,url);
two(win);
}
function one(win,url) {
win.open(url);
}
function two(win) {
win.close();
}

Categories

Resources