scope

Accessing a variable across different scopes in Javascript

こ雲淡風輕ζ 提交于 2020-01-04 09:16:18
问题 var todoList = { todos: [] displayTodos: function () { console.log(this.todos[i].todoText); } addTodo: function (todoText) { this.todos.push({todoText: todoText}); } } This isn't the complete program, but my question is this: In the displayTodos function, how am I able to access todoText (in the console log line)? Shouldn't the todoText variable that's declared in the addToDo function be limited in scope to the function in which it's declared, addToDo? Thanks 回答1: This has to do with the

in coldfusion, variables in what scope can be passed to and iframe page?

白昼怎懂夜的黑 提交于 2020-01-04 08:14:22
问题 i wrote 2 pages to test this problem, but the server complaints error. i don't know why, anyone can explaint it? great thanks. this is 1.cfm <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <title>Page Title</title> </head> <body> <cfscript> a="aaaaaaaaaaa"; b="bbbbbbbbbbb"; request.r1="rrrrrrr111111111"; request.r2="rrrrrrrr222222222"; session.s1=

ActiveRecord builds instance of wrong class through a scope targeting an STI class

强颜欢笑 提交于 2020-01-04 06:44:17
问题 I would like to be able to call the build method on a scope that targets a certain class of model via its STI type, and have ActiveRecord build an instance of the correct class. class LineItem < ActiveRecord::Base scope :discount, where(type: 'DiscountLineItem') end class DiscountLineItem < LineItem; end > LineItem.discount.build # Expect an instance of DiscountLineItem here => #<LineItem ...> Here, I expected an instance of DiscountLineItem , not an instance of LineItem . 回答1: Even though

c++ Linked List losing data between functions

江枫思渺然 提交于 2020-01-04 06:26:11
问题 I am having a problem with pointers and scope. I am trying to maintain an array of linked lists of pointers to objects. When I try to push_front() in one function, it works. However, if I try to iterate through the list in another part of my program, the list no longer contains any data, and my pointers are bad. This is part of my parseCommands function. the problem is when printList is called: Administrator *adminPtr = new Administrator(); // create new Administrator pointer //local

How do I Lock a javascript function to the local scope ? I want to prevent it from using global variables

为君一笑 提交于 2020-01-04 03:18:11
问题 Is there a way to prevent a function from using global variables like document , window , navigator , and other declared global functions ? EDIT1: And if I could choose which global objects that are restricted it would be great, because I would like to allow the function to use for an example the Math object and it's functions... 回答1: Is there a way to prevent a function from using global variables like document, window, navigator, and other declared global functions? No, unless... The only

AngularJS - Cannot access RootScope

吃可爱长大的小学妹 提交于 2020-01-04 02:41:16
问题 Strange problem, I cannot access the $rootScope in CtrlB variable that is getting set in CtrlA. HTML : <div role="main" class="container_9" ng-controller="CountryCtrl" ng-init="updateToken('<?php echo $TOKEN; ?>')"> CtrlA app.controller('CountryCtrl', function ($scope,$rootScope, $http) { $scope.updateToken = function(token) { $rootScope.token = token; } }); CtrlB app.controller('DealerListCtrl', function ($scope, $http, $rootScope, dealerService) { $scope.dealer = []; $http.get('files

How can I include a variable inside a callback function?

烂漫一生 提交于 2020-01-03 22:26:54
问题 I'm trying to get counts of array values greater than n . I'm using array_reduce() like so: $arr = range(1,10); echo array_reduce($arr, function ($a, $b) { return ($b > 5) ? ++$a : $a; }); This prints the number of elements in the array greater than the hard-coded 5 just fine. But how can I make 5 a variable like $n ? I've tried introducing a third argument like this: array_reduce($arr, function ($a, $b, $n) { return ($b > $n) ? ++$a : $a; }); // ^ ^ And even array_reduce($arr, function ($a,

Python: Expanding the scope of the iterator variable in the any() function

陌路散爱 提交于 2020-01-03 19:43:27
问题 I wrote some structurally equivalent real world code, where I anticipated the result for firstAdjective would be quick . But the result shows that word is out of scope. What is a neat solution that will overcome this but still retain the 'linguistic' style of what I want to do? >>> text = 'the quick brown fox jumps over the lazy dog' >>> adjectives = ['slow', 'quick', 'brown', 'lazy'] >>> if any(word in text for word in adjectives): ... firstAdjective = word ... Traceback (most recent call

Does swift allow code blocks without conditions/loops to reduce local variable scope? [duplicate]

妖精的绣舞 提交于 2020-01-03 16:47:13
问题 This question already has answers here : How to create local scopes in Swift? (5 answers) Closed 3 years ago . In languages with block level scope, I sometimes create arbitrary blocks just so I can encapsulate local variables and not have them pollute their parents' scope: func myFunc() { // if statements get block level scope if self.someCondition { var thisVarShouldntExistElsewhere = true self.doSomethingElse(thisVarShouldntExistElsewhere) } // many languages allow blocks without conditions

How can I pass an argument to a function called using setTimeout?

我怕爱的太早我们不能终老 提交于 2020-01-03 11:13:09
问题 I want to pass an argument to a function called using setTimeout . I have found these three options: A = 1; // Method 1: closure things setTimeout(function() { whatsA(A); }, 100); // Method 2: third argument (same result with [A]) setTimeout(whatsA, 100, A); // Method 3: eval setTimeout('whatsA(' + A + ')', 100); A = 2; function whatsA(X) { console.log(X); } This shows 2 , undefined , and 1 in Internet Explorer 9. Method 1 : Clearly, I would not like the argument to be changed after passing