anonymous-function

Javascript 'colon' for labeling anonymous functions?

大兔子大兔子 提交于 2019-11-28 03:27:04
What does this code refer too? queryString: function() { //some code } I tested it in the WebConsole (Firefox) but it wouldn't execute, so I'm thinking that it isn't equivalent to function queryString() {} . So what is it exactly? You are missing some code there, but I assume its part of an object declaration like this: var obj = { queryString: function() { //some code } }; obj.queryString(); It assigns a function as a property of an object literal. It would be equivalent to this: var obj = {}; obj.queryString = function() { ... }; obj.queryString(); In general, the object literal syntax looks

Use keyword in functions - PHP [duplicate]

↘锁芯ラ 提交于 2019-11-28 03:18:25
Possible Duplicate: In Php 5.3.0 what is the Function “Use” Identifier ? Should a sane programmer use it? I've been examining the Closures in PHP and this is what took my attention: public function getTotal($tax) { $total = 0.00; $callback = function ($quantity, $product) use ($tax, &$total) { $pricePerItem = constant(__CLASS__ . "::PRICE_" . strtoupper($product)); $total += ($pricePerItem * $quantity) * ($tax + 1.0); }; array_walk($this->products, $callback); return round($total, 2); } And somebody please give me an explanation about the usage of use in this code. function ($quantity,

Which version of php added anonymous functions

ぐ巨炮叔叔 提交于 2019-11-28 03:02:10
问题 In manual there is create_function function and you can pass result from that function to array_map , I thought that that is the only way to have something like anonymous functions and closures, but then I found that I can just put function like in javascript array_map(function($a) { return $a + 1; }, array(1, 2, 3, 4, 5)); In which version of php I can do this? Was this always there? 回答1: Closures (anonymous functions) were added in PHP 5.3.0 回答2: Anonymous functions are available since PHP

JavaScript: access variables inside anonymous function from the outside

做~自己de王妃 提交于 2019-11-28 02:37:37
问题 Say I have this anonymous function: (function(window){ var private = 'private msg'; function sayit() { alert(private) // works } document.body.onclick = sayit; // works })(window); // private shouldn't be accessible here Is this how JavaScript should behave? That is, there is no way to access private from anywhere outside of that anonymous function? If so, is it possible to find some kind of hack to access private from the outside, leaving the code the way it is? 回答1: Yes, this is how

Immediately-Invoked Function Expression (IIFE) vs not

半腔热情 提交于 2019-11-28 02:13:18
I see a lot of code like: var myApp ={}; (function() { console.log("Hello"); this.var1 = "mark"; //"this" is global, because it runs immediately on load. Caller is global myApp.sayGoodbye = function() { console.log("Goodbye"); }; })(); Which causes the anonymous function to execute immediately. But what is the advantage of this, compared to just putting the code inline? var myApp ={}; console.log("Hello"); var1 = "mark"; myApp.sayGoodbye = function() { console.log("Goodbye"); }; Apparently it's to do with scope of the function, but as the function is anonymous and called by window, it's scope

Sending anonymous functions through socket.io?

泄露秘密 提交于 2019-11-28 01:44:27
问题 I want to create a client-side function that can receive and execute arbitrary commands using client-side variables. I will be sending these functions from my server by using socket.io to send a JSON object containing an anonymous function which will be my command. It looks something like the following: //client side socket.on('executecommand', function(data){ var a = "foo"; data.execute(a); //should produce "foo" }); //server side socket.emit('executecommand', {'execute': function(param){

PHP use() function for scope?

自闭症网瘾萝莉.ら 提交于 2019-11-28 00:31:15
问题 I have seen code like this: function($cfg) use ($connections) {} but php.net doesn't seem to mention that function. I'm guessing it's related to scope, but how? 回答1: use is not a function, it's part of the Closure syntax. It simply makes the specified variables of the outer scope available inside the closure. $foo = 42; $bar = function () { // can't access $foo in here echo $foo; // undefined variable }; $baz = function () use ($foo) { // $foo is made available in here by use() echo $foo; //

javascript: Using the current for-loop counter-value inside a function() { }?

倾然丶 夕夏残阳落幕 提交于 2019-11-28 00:25:07
on a website i want to do this: (simplified) myHandlers = new Array(); for(var i = 0; i < 7; i++) { myHandlers.push(new Handler({ handlerName: 'myHandler'+i, // works, e.g. ->myHandler1, 2, 3 etc. handlerFunc: function(bla) { /*...*/ alert(i); } // doesn't work,all return 7 } } I could set the counter as another attribute of my Handler (which would copy the current value) and use it inside my function, but I guess, there is also a way to actually copy this value, no? When handlerFunc is called, the i inside the function refers to the i of the for loop. But that i does probably not have the

How can I define an anonymous generic Scala function?

六眼飞鱼酱① 提交于 2019-11-27 23:39:03
问题 Let's say I have this: val myAnon:(Option[String],String)=>String = (a:Option[String],defVal:String) => { a.getOrElse(defVal) } Don't mind what the function does. Is there anyway of making it generic, so I can have an Option[T]? 回答1: To summarize from that answer: No, you can't make anonymous functions generic, but you can explicitly define your function as a class that extends one of the Function0, Function1, Function2, etc.. traits and define the apply function from those traits. Then the

Wait for result of Async Volley request and return it

核能气质少年 提交于 2019-11-27 21:44:14
Below is a method in which I am trying to retrieve an user object by calling getSelf(). Problem is that the result is always null since the Volley request has not finished at the time of returning the result. I'm somewhat new to async processes, so I am not sure of the best way to have the method wait for the result of the API call to return the UserBean object. Can anyone give me some help? public UserBean getSelf(String url){ RpcJSONObject jsonRequest = new RpcJSONObject("getSelf", new JSONArray()); JsonObjectRequest userRequest = new JsonObjectRequest(Request.Method.POST, url, jsonRequest,