如何为函数建立全局变量 - Polymer
How to establish global variables for functions - Polymer
我已经搜索了这里的一些文档,但我不太明白。
基本上我只想为这个特定文件全局设置一些变量(就像局部全局变量一样),所以我不需要为我使用的每个函数都定义它们,因为它会变得相当又长又累。
任何建议都很棒!
我知道在这种情况下全局变量和事情很难在 Polymer 中完成,但我希望我能学会如何做,因为它会节省我花了很多时间和精力。
原JS
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 给定的属性字段中。
properties: {
response : {
type : Object
},
editButton: {
type: Object
},
//more declarations
},
ready: function () {
this.editButton = this.$.editInfo;
//more assignations
}
在你的情况下,我会直接使用 this.$.editInfo
,根据 'KISS' 原则没有辅助变量。
第二种方法太丑了,但是对于定义全局变量,您可以使用 window 对象来设置变量。
我已经搜索了这里的一些文档,但我不太明白。
基本上我只想为这个特定文件全局设置一些变量(就像局部全局变量一样),所以我不需要为我使用的每个函数都定义它们,因为它会变得相当又长又累。
任何建议都很棒!
我知道在这种情况下全局变量和事情很难在 Polymer 中完成,但我希望我能学会如何做,因为它会节省我花了很多时间和精力。
原JS
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 给定的属性字段中。
properties: {
response : {
type : Object
},
editButton: {
type: Object
},
//more declarations
},
ready: function () {
this.editButton = this.$.editInfo;
//more assignations
}
在你的情况下,我会直接使用 this.$.editInfo
,根据 'KISS' 原则没有辅助变量。
第二种方法太丑了,但是对于定义全局变量,您可以使用 window 对象来设置变量。