Javascript回调中的作用域
我希望在第二个Javascript回调中的作用域,javascript,polymer,polymer-1.0,Javascript,Polymer,Polymer 1.0,我希望在第二个控制台.log(this.items)上看到数组[“foo”,“bar”]。但是,我得到了未定义的 如何获得[“foo”,“bar”]而不是未定义的 http://jsbin.com/hodegimohu/edit?html,控制台,输出 <!doctype html> <head> <meta charset="utf-8"> <base href="https://polygit.org/components/">
控制台.log(this.items)
上看到数组[“foo”,“bar”]
。但是,我得到了未定义的
如何获得[“foo”,“bar”]
而不是未定义的
http://jsbin.com/hodegimohu/edit?html,控制台,输出
<!doctype html>
<head>
<meta charset="utf-8">
<base href="https://polygit.org/components/">
<script src="webcomponentsjs/webcomponents-lite.min.js"></script>
<link href="polymer/polymer.html" rel="import">
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
</head>
<body>
<dom-module id="x-element">
<script>
(function(){
Polymer({
is: 'x-element',
properties: {
items: {
type: Array,
value: ['foo','bar']
},
},
ready: function(){
console.log(this.items) // ["foo", "bar"]
google.charts.load('current', {
'packages': ['geochart']
});
google.charts.setOnLoadCallback(this._drawRegionsMap);
},
_drawRegionsMap: function() {
console.log(this.items); // undefined
},
});
})();
</script>
</dom-module>
<x-element></x-element>
</body>
(功能(){
聚合物({
是‘x元素’,
特性:{
项目:{
类型:数组,
值:['foo','bar']
},
},
就绪:函数(){
console.log(this.items)/[“foo”,“bar”]
google.charts.load('current'{
“包”:[“地球艺术”]
});
google.charts.setOnLoadCallback(this.\u DrawRegionMap);
},
_DrawRegionMap:function(){
console.log(this.items);//未定义
},
});
})();
只需使用.bind
google.charts.setOnLoadCallback(this._drawRegionsMap.bind(this));
只需适当地处理回调(不基于聚合物)。只需使用.bind
google.charts.setOnLoadCallback(this._drawRegionsMap.bind(this));
只不过是适当地处理回调(不基于聚合物)