scope

How can I make the variable newUser defined inside a function global?

可紊 提交于 2019-12-24 03:29:49
问题 I send the variable newUser from options.html to background.html with chrome.extension.getBackgroundPage() like this document.getElementById("save").addEventListener( "click", function () { var newUser = document.getElementById("getEmail").value; var bkg = chrome.extension.getBackgroundPage(); bkg.saveNewUser(newUser); console.log("newUser " + newUser); } , false) In background.html I have function saveNewUser (newUser) { newUser = newUser; //this should be global? console.log("newUser from

angularjs foreach loop through http requests dependent on previous request

一个人想着一个人 提交于 2019-12-24 03:09:28
问题 I want to loop through an array to perform $http.jsonp requests in angular, however each request will be slightly different depending on the timestamp of the previous $http.jsonp request. I'm trying to loop through 5 requests and each request is dependent on the previous request info. How can I perform a foreach loop that waits for each $http request to finish so that the "lastTime" variable is updated properly for the next $http request in the loop? Here's my code: var lastTime = null;

Javascript closures - behavior of overridden functions from the global scope

别来无恙 提交于 2019-12-24 02:18:07
问题 This question is more on javascript principle. function done(){ console.log('done defined with `function done(){ ...`'); } var done = function(){ console.log('done defined with `var done = ...`'); } done = function(){ console.log('without `var`, just `done = ...`'); } If defined right inside <script> tags, will they all do the same thing, right? But if I place them in a closure (function(){ function definintion goes here }()) will any of these three types override either the globally defined

Setting anonymous function scope of 'this' in jQuery utility / ajax methods

让人想犯罪 __ 提交于 2019-12-24 01:27:20
问题 As noted in this blog post you can set the scope of this in an anonymous function in Javascript. Is there a more elegant way of scoping this in the anonymous function call on success of the AJAX request (i.e. not using that )? For example: var Foo = { bar: function(id) { var that = this; $.ajax({ url: "www.somedomain.com/ajax_handler", success: function(data) { that._updateDiv(id, data); } }); }, _updateDiv: function(id, data) { $(id).innerHTML = data; } }; var foo = new Foo; foo.bar('mydiv')

how to have global variables among different modules in Python

好久不见. 提交于 2019-12-24 01:07:18
问题 I investigated that scope of global variables in python is limited to the module. But I need the scope to be global among different modules. Is there such a thing? I played around __builtin__ but no luck. thanks in advance! 回答1: You can access global variables from other modules by importing them explicitly. In module foo : joe = 5 In module bar : from foo import joe print joe Note that this isn't recommended, though. It's much better to hide access to a module's variables by using functions.

Can't get $_POST values (php, html)

Deadly 提交于 2019-12-24 00:57:51
问题 I can't get the $_POST['name'] values sent from an html form on my php file. I've seen lots of similar questions but nothing helped. I have lot's of includes so I believe it's a scope issue, but I can't figure it out. index.php print_r($_POST); //Returns nothing, tried other ways too //lot's of variables being defined include 'sql_data_handlers.php'; //instantiating some things sql_data_handlers.php //some functions retrieving data from sql db and finally: include($DOCUMENT_ROOT . "page.html"

R: How can a function assign a value to a variable that will persist outside the function?

那年仲夏 提交于 2019-12-24 00:53:58
问题 This is probably easy but I am confused as hell with environments. I would like to use a call to a function to assign a value to a variable, but I need to be able to use that variable after the call. However, I assume, the variable created by the function only exist within the function environment. In short, I need something akin to a global variable (but I am a beginner with R). The following code : assignvalue = function(varname){ assign(varname,1) } assignvalue("test") test returns Error:

Is it possible in Javascript to create an external closure?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 00:49:08
问题 Normally, to create a closure, you create it inside another function, and it gets the scope of its parent: var parent = function(){ var a = "works!"; var subfunction(){ console.log(a); // "works!" } subfunction(); } I'm trying to figure out a way to emulate this closure behavior with a function that is defined outside of the parent function. I know this is possible using parameters: var parent = function(){ var a = "hello"; subfunction(a); } var subfunction(a){ console.log(a); // works, but

How can I keep the value of this variable for my next subroutine?

僤鯓⒐⒋嵵緔 提交于 2019-12-24 00:48:00
问题 I'm a beginning programmer (no experience) learning Visual Basic for a job I'm doing right now. I've been reading for a day or so and have finally decided to start making the required program! However, I'm running into some problems. Right now I have two subroutines. The first subroutine lets the user input how many data pairs they have so that I can create a table for them to fill in. This is so their data is in the right place for me to reference it later. There is then a button they press

Javascript - variable scope in event handler

走远了吗. 提交于 2019-12-24 00:33:36
问题 Can someone clarify the my understanding of variable scope within event handlers? Take a look at the code below: var address = new Array(); address[0] = '1 Smith Street'; address[1] = '2 Smith Street'; for(var rownum=0; rownum<=address.length; rownum++) { if(address[rownum]) geocoder.geocode( {'address': address[rownum]}, geocodeCallBack); } function geocodeCallBack(results, status) { var marker = new google.maps.Marker({ map: map, position: results[0].geometry.location, title: results[0]