最近我想出了以下问题:
在我所有html页面的网站中,我在body onLoad事件中调用了一个函数:
<body onLoad="func1();">
Run Code Online (Sandbox Code Playgroud)
这是我的html模板的一部分,因此它出现在我网站的每个页面上,我无法改变它.现在,交易是在某些页面上,我需要调用一些其他函数onload,我尝试使用window.onload属性,但它擦除了func1的调用...
我现在可以说:
window.onload = func2(); //where func2() calls to func1()
Run Code Online (Sandbox Code Playgroud)
但这看起来很脏又蹩脚?不是吗?
那么,有没有办法将一些函数添加到即将执行onload的那些函数而不删除旧函数?另外我使用asp.net,如果这可以帮助...
谢谢!
我刚刚学习如何最好地组织我的javascript代码,我对我写的这小段代码有一个疑问:
var reportsControllerIndex = {
plotMapPoints: function(data) {
//plots points
},
drawMap: function() {
$.getJSON('/reports.json', function(data) {
reportsControllerIndex.plotMapPoints(data);
});
},
run: function() {
reportsControllerIndex.drawMap();
}
};
Run Code Online (Sandbox Code Playgroud)
问题是关于从reportsControllerIndex对象中调用reportsControllerIndex的另一个函数.我首先尝试了以下运行函数的代码:
run: function() {
this.drawMap();
}
Run Code Online (Sandbox Code Playgroud)
这很完美.但是,我很快发现这是为drawMap函数做的:
drawMap: function() {
$.getJSON('/reports.json', function(data) {
this.plotMapPoints(data);
});
}
Run Code Online (Sandbox Code Playgroud)
不起作用,因为"this"现在将引用getJSON调用的回调函数.
我的解决方案是将reportsControllerIndex放在我想要调用的所有方法的前面,但我很好奇:是否有一种更相对的方式来调用像这样的整体对象中的函数(就像你在一个类中所做的那样)标准的OO语言)?或者我现在被迫这样做,只是通过对象的名称调用方法?
我的理解是,用C#连接的任何事件处理程序都需要无线连接.
Object myObject = new Object();
myObject.Event += EventHandler; //Wired
myObject.Event -= EventHandler; //Unwired
Run Code Online (Sandbox Code Playgroud)
但是你需要取消以下代码吗?如果是的话,怎么样?
Object myObject = new Object();
myObject.Event += (object sender, EventArgs e) => { }; //Wired
myObject.Event -= ????? //Unwire? How?
Run Code Online (Sandbox Code Playgroud)
我的假设是不是?
有没有一种方法可以将对象上下文传递给匿名函数而不$this作为参数传递?
class Foo {
function bar() {
$this->baz = 2;
# Fatal error: Using $this when not in object context
$echo_baz = function() { echo $this->baz; };
$echo_baz();
}
}
$f = new Foo();
$f->bar();
Run Code Online (Sandbox Code Playgroud) 我见过这样的代码:
function($cfg) use ($connections) {}
Run Code Online (Sandbox Code Playgroud)
但是php.net似乎没有提到这个功能.我猜它与范围有关,但是怎么样?
我想要一个函数(例如,拟合函数)来返回一个struct我可以保存并稍后使用的匿名函数(通常存储在a中).但是,传递@func往往会传递函数指针而不是函数本身.是一个inline函数来做到这一点的唯一途径?我想避免inline因为它非常慢.
如果这个问题不明确,这里有一个有问题的代码示例:我testFunc.m在一些文件中写了一个文件PATH
%testFunc.m
function myfunc = testFunc()
myfunc = @(x) x.^2;
end
Run Code Online (Sandbox Code Playgroud)
然后我将函数存储在一个struct.(我知道这应该是一个对象!)
>> mystruct = struct;
>> mystruct.func = testFunc()
>> mstruct.x = [1 2 3];
>> save('myfile.mat','mystruct')
>> mystruct.func(mystruct.x)
ans =
1 4 9
Run Code Online (Sandbox Code Playgroud)
如果我然后移动myfile.mat或testFunc.m加载 myfile.mat,我无法加载旧的结构.相反,我得到错误:
>> cd 'otherdir'
>> load('../myfile.mat')
Warning: Could not find appropriate function on path
loading function handle PATH/testFunc.m>@(x)x.^2
Run Code Online (Sandbox Code Playgroud)
我知道有问题因为,如果我检查一下 functions
>> functions(mystruct.func)
ans =
function: …Run Code Online (Sandbox Code Playgroud) 我倾向于以下列方式编写对象构造函数:
function Person(name) {
this.name = name;
}
Person.prototype.greet = function () {
alert("Hello! My name is " + this.name + ".");
};
Run Code Online (Sandbox Code Playgroud)
我注意到一些JavaScript库和框架添加了一些额外的代码,如下所示:
var Person = (function () {
function Person(name) {
this.name = name;
}
Person.prototype.greet = function () {
alert("Hello! My name is " + this.name + ".");
};
return Person;
})();
Run Code Online (Sandbox Code Playgroud)
我知道自执行匿名函数的作用和用途.我目前未能看到的是,在定义构造函数及其原型时,它提供了哪些优势或好处.
编辑#1:
我知道模块模式及其优点,并且在我的编码中经常使用它.我在沟通中的错误并不清楚我的第一个代码示例不应该在全球范围内.我总是将所有外部JavaScript文件包装在一个自执行的匿名函数中,以强制执行代码的本地范围.
例如:
;(function ( window, undefined ) {
var p = function (name) {
this.name;
};
p.prototype.greet = function () {
alert("Hello! My …Run Code Online (Sandbox Code Playgroud) javascript constructor anonymous-function function-prototypes module-pattern
只是想知道为什么这样的东西不起作用:
public function address($name){
if(!isset($this->addresses[$name])){
$address = new stdClass();
$address->city = function($class = '', $style = ''){
return $class;
};
$this->addresses[$name] = $address;
}
return $this->addresses[$name];
}
Run Code Online (Sandbox Code Playgroud)
把它称为echo $class->address('name')->city('Class')应该回应Class,但是我得到了Fatal error: Call to undefined method stdClass::city()
我可以找到一个更好的方法来做到这一点,因为这会变得混乱,但我想知道我在那里做错了什么,或者PHP不支持这个以及为什么.
我不确定如何标题,但请考虑以下lua代码:
print(function ()
s = ""
for i = 1, 10 do
s = s..tostring(i)
end
return s
end)
Run Code Online (Sandbox Code Playgroud)
但是这只打印了函数地址,因为function()返回一个闭包.有没有办法评估匿名函数?就像我可以在附加括号中使用lambda的方案一样?
((lambda ()(display "Hello World")))
Run Code Online (Sandbox Code Playgroud)
当然我知道,我可以事先定义函数并稍后调用它,但我很好奇是否可以在lua中使用它.提前致谢所有回复.
基本上static 关键字的目的对我来说是完全清楚的,PHP 文档仅解释了类上下文中关键字的目的。我注意到我的 IDE 插件之一建议我应该将许多回调函数声明为静态函数。
无静态:
$myUniqueArray = unique($arrayToFilter,
function (ExamQuestion $examQuestion) {
return $examQuestion->getId();
}
);
Run Code Online (Sandbox Code Playgroud)
与静态:
$myUniqueArray = unique($arrayToFilter,
static function (ExamQuestion $examQuestion) {
return $examQuestion->getId();
}
);
Run Code Online (Sandbox Code Playgroud)
对于结果来说,这没有什么区别,两者都有效。PHP 中的回调函数和静态回调函数到底有什么区别?在这种情况下可能有哪些好处和缺点?