Javascript 如何建立函数的全局变量-聚合物
我已经翻遍了这里的一些文件,但我还是不太明白 基本上,我只想为这个特定的文件全局地设置一些变量(就像局部全局变量一样),所以我不需要为我使用的每个函数都定义它们,因为这样做会变得很长很累 任何建议都太棒了 我知道全局变量和事情在这种情况下很难做到,但我希望我能学会如何做到这一点,因为这将节省我大量的时间和精力。 原创JSJavascript 如何建立函数的全局变量-聚合物,javascript,html,polymer,Javascript,Html,Polymer,我已经翻遍了这里的一些文件,但我还是不太明白 基本上,我只想为这个特定的文件全局地设置一些变量(就像局部全局变量一样),所以我不需要为我使用的每个函数都定义它们,因为这样做会变得很长很累 任何建议都太棒了 我知道全局变量和事情在这种情况下很难做到,但我希望我能学会如何做到这一点,因为这将节省我大量的时间和精力。 原创JS Polymer({ is: 'settings-page', properties: { response : { type : O
Polymer({
is: 'settings-page',
properties: {
response : {
type : Object
},
},
/* have iron pages show the saved state of each section to start with */
ready: function () {
this.selected = 'saved';
this.selection = 'saved';
},
editDetails : function () {
/* click edit | open edit mode | edit icon disappears | save & cancel icons appear */
var editButton = this.$.editInfo;
var saveButton = this.$.saveInfo;
var cancelButton = this.$.clearInfo;
saveButton.classList.add('show');
cancelButton.classList.add('show');
editButton.classList.add('hide');
},
/* There are saveDetails and cancelDetails functions below doing
pretty much the same stuff. */
JS在线查看示例后
(function() {
var data = {
editInfo: this.$.editInfo,
saveInfo: this.$.saveInfo,
cancelInfo: this.$.clearInfo
}
Polymer({
is: 'settings-page',
properties: {
response : {
type : Object
},
},
/* have iron pages show the saved state of each section to start with */
ready: function () {
this.selected = 'saved';
this.selection = 'saved';
this.data = data;
},
editDetails : function () {
/* click edit | open edit mode | edit icon disappears | save & cancel icons appear */
saveButton.classList.add('show');
cancelButton.classList.add('show');
editButton.classList.add('hide');
},
/* There are saveDetails and cancelDetails functions below doing
pretty much the same stuff. */
不幸的是,在polymer中没有方法声明全局变量,您有两种方法 在我看来,最好的方法是将变量声明到由polymer给出的properties字段中
properties: {
response : {
type : Object
},
editButton: {
type: Object
},
//more declarations
},
ready: function () {
this.editButton = this.$.editInfo;
//more assignations
}
在您的情况下,我将直接使用this.$.editInfo
,根据“KISS”原则,不使用辅助变量
第二种方法非常难看,但是对于定义全局变量,您可以使用window对象来设置变量。谢谢您的输入Viltor。我曾考虑过不要声明变量并以原始形式使用它们。我只是在考虑以后的可读性和维护等问题。看看铁元素。