JavaScript API不能在函数内工作
问题描述:
我正在使用第三方API来获取值。例如,在这里得到一个国家的价值就是样本。JavaScript API不能在函数内工作
var _ttprofiles = _ttprofiles || [];
_ttprofiles.profiles = [];
_ttprofiles.push(['_sync', false]);
var callback = function(profiles) {
console.log(profiles.getCountry);
};
_ttprofiles.push(['_registerCallback', callback]);
_ttprofiles.push(['_enableServices']);
现在我很简单地在这样的JavaScript文件中使用。
var _ttprofiles = _ttprofiles || [];
_ttprofiles.profiles = [];
_ttprofiles.push(['_sync', false]);
var callback = function(profiles) {
console.log(profiles.getCountry);
};
_ttprofiles.push(['_registerCallback', callback]);
_ttprofiles.push(['_enableServices']);
(function() {
try {
// getValues();
} catch (err) {
}
})();
我在该国获得价值。这里1是我的萤火虫
的屏幕快照但是,当我把这个方法中的,然后我得到空字符串
(function() {
try {
getValues();
} catch (err) {
}
})();
function getValues()
{
var _ttprofiles = _ttprofiles || [];
_ttprofiles.profiles = [];
_ttprofiles.push(['_sync', false]);
var callback = function(profiles) {
console.log(profiles.getCountry);
};
_ttprofiles.push(['_registerCallback', callback]);
_ttprofiles.push(['_enableServices']);
}
然后我得到空字符串,为什么?这里是萤火虫
的屏幕截图,可以有人建议我的JavaScript调试好。我在Firefox中使用萤火虫,但我的控制台不工作我也尝试铬调试器,但它有同样的问题,虽然控制台的萤火虫在铬工作
答
看起来像Nicolas一样,但是你必须创建一个位于全局命名空间中的_ttprofiles变量,而不是像第三方库已经创建该变量那样。所以,我试着把它传递给你的函数调用,它似乎工作。
我不得不承认我不完全确定输出应该是什么。我在警报框中找到了'我们'。
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="http://d.tailtarget.com/profiles.js"></script>
</head>
<body>
<script>
(function() {
try {
getValues(_ttprofiles);
} catch (err) {
console.log(err);
}
})();
function getValues(){
_ttprofiles = _ttprofiles || [];
_ttprofiles.profiles = [];
_ttprofiles.push(['_sync', false]);
var callback = function(profiles) {
alert(profiles.getCountry);
};
_ttprofiles.push(['_registerCallback', callback]);
_ttprofiles.push(['_enableServices']);
}
</script>
</body> </html>
似乎没有要在这个问题上的信息足以让一个有意义的答案。什么是第三方API? 'profiles'从哪里来? – TML 2014-09-24 04:58:40
我在我的索引页中使用所以配置文件来自他们的 – Coder 2014-09-24 05:00:31
我不明白'var _ttprofiles = _ttprofiles || [];',从哪里得到'_ttprofiles'? – 2014-09-24 05:01:14