scope

Updating a global variable from a function

廉价感情. 提交于 2020-01-06 17:42:26
问题 There is a global variable called numbers. Function one calculates a random number and stores it in mynumber variable. var mynumber; function one (){ var mynumber = Math.floor(Math.random() * 6) + 1; } I need to use it in a separate function as belows. But an error says undefined - that means the value from function one is not updated. function two (){ alert(mynumber); } How to overcome this problem. Is there a way to update the mynumber global variable. 回答1: If you declare a variable with

How to :conceal from Feed?

微笑、不失礼 提交于 2020-01-06 17:09:07
问题 There are many valuations, activities, and users. Each table has this line: t.boolean "conceal", default: false When submitting a valuation it can be made true: pry(main)> Valuation.find(16) Valuation Load (0.1ms) SELECT "valuations".* FROM "valuations" WHERE "valuations"."id" = ? LIMIT 1 [["id", 16]] => #<Valuation:0x007fbbee41cf60 id: 16, conceal: true, user_id: 1, created_at: Thu, 23 Apr 2015 20:24:09 UTC +00:00, updated_at: Thu, 23 Apr 2015 20:24:09 UTC +00:00, likes: nil, name: "CONCEAL

How to set object property in prototype function (scope problem)?

 ̄綄美尐妖づ 提交于 2020-01-06 15:26:09
问题 This is something trivial, which I've forgotten. There are possibly duplicates - I searched a little, found similar, but couldn't find as concise. String.prototype.test = function(){this.bar = this.length + 2;} var str = "foo"; str.test(); console.log(str); // foo console.log(str.bar); // undefined Pretty sure it has to do with this being trapped in the closure. 回答1: Has to do with the way you're creating your string in this case. Try: var str = new String("Foo"); and you'll find it magically

Global Scope when accessing array element inside function

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-06 12:50:17
问题 When I assign a value into an array the scope of the variable remain local (see loc() ). However if I access the element of an array the scope becomes global ( see glob() ) import numpy as np M = np.array([1]) def loc(): M = 2 return 0 def glob(): M[0] = 3 return 0 loc() print M >>> [1] glob() print M >>> [3] Why does this happen ? How can i locally modify the elements of an array without modifying the array globally? I need to have a loop inside my function changing one element at a time.

javascript 'this' scope in jQuery

痞子三分冷 提交于 2020-01-06 08:46:13
问题 I have just converted an piece of code that was an object literal to a class and am having problems with the scope in a jQuery $.each() loop. Say I have a class... var myClass = function(var1) { this.var1 = var1; } myClass.prototype.myFuncion = function() { var context = this; $.each(this.var1, function() { context.myOtherFunction() //is there any way of accessing 'this' from here? }) } I want to know how to access the class context from within the each? I know I can define a variable outside

Name age does not exist in the current context

一笑奈何 提交于 2020-01-06 08:28:14
问题 I have an assignment to write a simple program in C# on Visual Studio that reads your birth date from the console, prints back your age now and your age after ten years. I've found a Microsoft tutorial and decided to use it but it went wrong. Here's the tutorial: http://channel9.msdn.com/Series/Twenty-C-Questions-Explained/14 I'm not done with the task and I see the following errors: 'The name age does not exist in the current context' . namespace AgeAfterTenYears { class AgeAfterTenYears {

Specify object to use as global scope in new Function constructor?

心不动则不痛 提交于 2020-01-06 08:04:28
问题 Is there a way in Javascript to specify an object to use as the global scope when creating a function using new Function(...) ? For instance, consider a string var str = "return foo()"; // user supplied, generated by a trusted program we can create a function with this string, passing in certain arguments var fn = new Function(str); var result = fn() This works if foo is defined in the global scope, either window in a browser or GLOBAL.global in Node.js. But is there a way to give this newly

extjs4 MVC Scope Issue

ε祈祈猫儿з 提交于 2020-01-06 07:58:10
问题 I am trying to call resetCombo method once i click on link from tooltip which is rendered on combo But i am not able to access it because of scope issue not sure what i am missing. Please help me on this. Ext.define('test.BasicForm', { extend: 'Ext.form.Panel', renderTo:Ext.getBody(), initComponent :function(){ this.items=[ { fieldLabel: 'Test', xtype: 'combo', displayField: 'name', width: 320, labelWidth: 130, store: [ [1, 'Value 1'], [2, 'Value 2'], [3, 'Value 3'], [4, 'Value 4'] ],

Scope of object within an object in Java

蓝咒 提交于 2020-01-06 05:44:13
问题 I'm learning Java at the moment so I hope this question isn't too obvious. I come from another language which does not have garbage collection. In this other language I sometimes created objects in constructor and then deleted them in the destructor so I could use them for the entire life of the object. As a simplified example, I have a user and a booking class. The booking class references a user but if I create the user in the constructor of the booking class, it dereferences the user once

Scope of variable in Javascript problem

橙三吉。 提交于 2020-01-06 04:59:26
问题 var query1 = urlencode($('input[name="searchTerm1"]').val()); //user1 var query2 = urlencode($('input[name="searchTerm2"]').val()); //user1 var rpp = 20; //number of tweets to retrieve out var c=0; var f1=new Array(); var f2=new Array(); var common=new Array(); $.getJSON('http://twitter.com/followers/ids.json?screen_name='+ query1 + '&callback=?', function(data) { f1=data; $('#content').append('p'+f1[0]+'p');//this one is coming }); $.getJSON('http://twitter.com/followers/ids.json?screen_name