_editor: function () {
//retrieve all the editors on the current page
var editors = window.tinymce.editors;
var container = this.element;
//pick one that's associated with current container
$(editors).each(function (i, ed) {
if (ed.id == container.id) {
return ed; // even if this is invoked,
}
});
// undefined is returned
}
我不得不将上面的代码更改为
_editor: function () {
//retrieve all the editors on the current page
var editors = window.tinymce.editors;
var container = this.element;
var editor;
$(editors).each(function (i, ed) {
if (ed.id == container.id) {
editor = ed; // do not return yet. Store it.
}
});
return editor; // return here
}
我认为这是因为 JavaScript 的作用域特性。有人可以解释 1) 如果这只是 JavaScript 固有的 2) 上面代码中每个功能范围到底发生了什么?
谢谢。
最佳答案
在第一种情况下,您从传递给 $(editors).each
的匿名函数返回一个值,而不是外部函数。在第二种情况下,您从外部函数返回。
这就是它与几乎所有允许嵌套函数的语言一起工作的方式。 return
只从最里面的函数返回。
关于javascript - 在javascript中的if子句中返回一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20557759/