作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个非常简单的 JavaScript 代码,应该使用参数 firstname
和 lastname
返回全名,当我在函数中提醒全名时,所有工作正常。但我正在努力如何使 fullname
变量可以从函数/实异常(exception)部访问?
这是我的代码:
var getName = function () {
this.name = function (firstname, lastname, success) {
var fullName = firstname + " " + lastname;
success(fullName);
};
};
var test = new getName();
test.name("John", "Smith", function (output) {
var fullname = output;
alert(fullname); //this works fine
return fullname; // I need this variable to be accessed from outside this function
});
var myName = fullname; //I need to create the variable here
alert(myName); //this does not work
这是Fiddle
非常感谢您的帮助。
编辑:我正在构建一个 iPad 应用程序,其中使用 cordova 和 javascript 插件。其中一个插件使我可以访问设备内的文件。现在,我需要能够获取路径并在回调之外使用它,以便我可以在范围内的任何位置使用:
这是插件代码:
var FileManager = function(){
this.get_path = function(todir,tofilename, success){
fail = (typeof fail == 'undefined')? Log('FileManager','read file fail'): fail;
this.load_file(
todir,
tofilename,
function(fileEntry){
var sPath = fileEntry.toURL();
success(sPath);
},
Log('fail')
);
}
this.load_file = function(dir, file, success, fail, dont_repeat){
if(!dir || dir =='')
{
Log('error','msg')('No file should be created, without a folder, to prevent a mess');
fail();
return;
}
fail = (typeof fail == 'undefined')? Log('FileManager','load file fail'): fail;
var full_file_path = dir+'/'+file;
var object = this;
// get fileSystem
fileSystemSingleton.load(
function(fs){
var dont_repeat_inner = dont_repeat;
// get file handler
console.log(fs.root);
fs.root.getFile(
full_file_path,
{create: true, exclusive: false},
success,
function(error){
if(dont_repeat == true){
Log('FileManager','error')('recurring error, gettingout of here!');
return;
}
// if target folder does not exist, create it
if(error.code == 3){
Log('FileManager','msg')('folder does not exist, creating it');
var a = new DirManager();
a.create_r(
dir,
function(){
Log('FileManager','mesg')('trying to create the file again: '+file);
object.load_file(dir,file,success,fail,true);
},
fail
);
return;
}
fail(error);
}
);
}
);
};
}
这是我的使用方法:
var b = new FileManager(); // Initialize a File manager
b.get_path('Documents','data.json',function(path){
myPath = path;
console.log(myPath); //this gives me the path of the file in the console, which works fine
return myPath; //I return the path to be accessed outside
});
var filePath = myPath; //here I need to access the path variable
console.log(filePath)// here, the path is undefined and it does not work
//Since I'm using angular services to retrieve the data from json, I'd like to pass the filePath
//this is a fraction of code for retrieving data:
return $resource('data.json',{}, {'query': {method: 'GET', isArray: false}});
//passing the value does not work
return $resource(filePath,{}, {'query': {method: 'GET', isArray: false}});
//even wrapping resource around instance does not work, it breaks the whole app
b.get_path('Documents','data.json',function(path){
myPath = path;
console.log(myPath); //this gives me the path of the file in the console, which works fine
return $resource(myPath,{}, {'query': {method: 'GET', isArray: false}});
});
工厂服务:
'use strict';
angular
.module ('myApp')
.factory('getMeData', function ($resource) {
var b = new FileManager(); // Initialize a File manager
b.get_path('Documents','data.json',function(path){
myPath = path;
return myPath;
});
//below doesn't work when passing path (it is undefined)
return $resource(myPath,{}, {'query': {method: 'GET', isArray: false}});
//when wrapping it around, the app crashes
b.get_path('Documents','data.json',function(path){
myPath = path;
return $resource(myPath,{}, {'query': {method: 'GET', isArray: false}});
});
//none of the solution above work
});
最佳答案
如果您的 name
函数返回 success
的结果,那么您可以使用该方法的返回值。
即
var getName = function () {
this.name = function (firstname, lastname, success) {
var fullName = firstname + " " + lastname;
return success(fullName);
};
};
var test = new getName();
var myName = test.name("John", "Smith", function (output) {
var fullname = output;
alert(fullname); //this works fine
return fullname; // I need this variable to be accessed from outside this function
});
alert(myName);
关于javascript - 如何从函数/实异常(exception)部访问变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27818260/
我需要计算像这样存储的 2 个短数据数组的 FFT(重复百万次): 等等。 数组值用黄色和蓝色表示。每个 K 值都有一个大小为 K 的未使用数据空间,我需要跳过。 我对数据进行了重新排序(和 floa
我是一名优秀的程序员,十分优秀!