anonymous-function

Passing anonymous function as callback in Javascript

那年仲夏 提交于 2019-12-05 12:05:19
Is there a way to pass anonymous function as a custom callback function in Javascript? I have this code: function notifyme(msg){ console.log(msg) } notifyme("msg", function(){ //do some custom redirect logic }); I am trying the above code and it's executing notifyme function, but not going further with redirect code. I know I can pass a function name as a callback, but I don't have a specific function that I can pass. This is why they invented the anonymous function I guess. It'd be really nice if there was a way to do that. Your code pattern should be like function notifyme(msg,callback){

Anonymous function for a method of an object [duplicate]

独自空忆成欢 提交于 2019-12-05 08:01:24
Possible Duplicate: Calling closure assigned to object property directly Why this is not possible in PHP? I want to be able to create a function on the fly for a particular object. $a = 'a'; $tokenMapper->tokenJoinHistories = function($a) { echo $a; }; $tokenMapper->tokenJoinHistories($a); With $obj->foo() you call methods, but you want to call a property as a function/method. This just confuses the parser, because he didn't find a method with the name foo() , but he cannot expect any property to be something callable. call_user_func($tokenMapper->tokenJoinHistories, $a); Or you extend your

How to get Scala function's parameters / return type?

强颜欢笑 提交于 2019-12-05 07:57:18
I have a function, and would like to obtain its parameter types and return type for use in Scala macros. scala> val fn = (a: String, b: Double) => 123 fn: (String, Double) => Int = <function2> scala> fn.getClass res1: Class[_ <: (String, Double) => Int] = class $anonfun$1 In the above example, the parameter types and return type already get printed at both lines, but I don't know how to access them. Even with toString I'd be stuck with the <function2> and class $anonfun$1 parts right of the = sign -- otherwise a bit of ugly string parsing might have done. I found that the MethodSymbolApi

JavaScript: When assigning an anonymous function to a variable, function return value is not passed, rather the function as a string

筅森魡賤 提交于 2019-12-05 05:54:34
I am trying to learn JavaScript but I've come across a hurdle. If the answer is obvious and reachable through a simple search I apologize in advance. I am a novice to programming and JavaScript, and unsure what line of inquiry to follow. In the following code, the function takes values from a HTML form, does some processing and sends them back. I've tested the input and output process and it's working correctly. function foo() { var x = parseInt(document.formdata.fieldone.value); var y = parseFloat(document.formdata.fieldtwo.value); if (isNaN(y)) { var z = x; } else { var z = function(x, y) {

Javascript variable scope in addEventListener anonymous function

安稳与你 提交于 2019-12-05 01:34:32
问题 When clicking on each div it should alert '1' if div 1 was clicked on or '5' if div 2 was clicked on. I have tried to make this code as easy to as possible because this is needed in a much larger application. <html> <head> <style type="text/css"> #div1 { background-color: #00ff00; margin: 10px; padding: 10px; } #div2 { background-color: #0000ff; margin: 10px; padding: 10px; } </style> <script type="text/javascript"> function init() { var total = 1; var div1 = document.getElementById('div1'),

Replacing anonymous functions with named function (in jQuery)

旧街凉风 提交于 2019-12-04 19:04:30
My original (working) code looks like: jQuery().ready(function ($) { $('[id="errorMessages"]').ajaxStart(function () { $(this).html(""); }); $('[id="errorMessages"]').ajaxError(function (e, jqxhr, settings, exception) { //... }); }); When I am trying to replace the anonymous functions into a named function calls like: (I am doing a POC for some requirement, which expects such implementation.) function fs() { $(this).html(""); } function fe(e, jqxhr, settings, exception) { //... } jQuery().ready(function ($) { $('[id="errorMessages"]').ajaxStart(fs()); $('[id="errorMessages"]').ajaxError(fe(e,

javascript object variable becomes undefined inside anonymous function

本秂侑毒 提交于 2019-12-04 17:40:36
问题 So I can't quite figure out why the variable this.tasks becomes undefined inside of the add event listener I have inside of my goal object. I have a feeling it might have something to do with asynchronous programming(which I still don't fully understand). Sorry I'm a bit of a JS noob, but if you guys could explain to me what I'm doing wrong and what might be a better solution that would be awesome! Thanks. function Goal(name) { this.gDiv = document.createElement('div'); this.name = name ||

Scala anonymous function syntax and return type

跟風遠走 提交于 2019-12-04 14:46:57
I have found few kinds of anonymous function syntax in scala: val m5_1 = { (n: Int) => n * 5 } val m5_2 = (n: Int) => { n * 5 } : Int val m5_3: Int => Int = n => { n * 5 } Is that all types or some more syntax kinds present? Are they all equivalent? Which one is more/less preferred? How can I specify the return type in m5_1 ? I'll try to add to @pamu's answer: Which one is more/less preferred? I'd say the second variant is a bit uncommon. When the type inference is easily visible, you can go for the first, otherwise being explicit as in the third case is good. You don't need braces there, so a

Why use (function(){})() or !function(){}()?

纵然是瞬间 提交于 2019-12-04 13:18:19
I was reading In JavaScript, what is the advantage of !function(){}() over (function () {})()? then it hit me, why use : (function(){})() or !function(){}() instead of just function(){}() ? Is there any specific reason? It depends on where you write this. function(){}() by itself will generate a syntax error as it is evaluated as function declaration and those need names. By using parenthesis or the not operator, you enforce it to be interpreted as function expression , which don't need names. In case where it would be treated as expression anyway, you can omit the parenthesis or the operator.

How to call a method inside a javascript object

故事扮演 提交于 2019-12-04 11:56:02
问题 I'm just learning about how to best organize my javascript code, and I had a question regarding this small piece of code I wrote: var reportsControllerIndex = { plotMapPoints: function(data) { //plots points }, drawMap: function() { $.getJSON('/reports.json', function(data) { reportsControllerIndex.plotMapPoints(data); }); }, run: function() { reportsControllerIndex.drawMap(); } }; The question is regarding calling another function of reportsControllerIndex from within the