[英]Ways to do String Addition inside Object Literal
Can I do some string additions inside an object literal? 我可以在对象文字中添加一些字符串吗?
Chrome gives me the following error message if I do height: 'calc(100% - ' + String(this.padding) + 'px * 2)'
. 如果我执行
height: 'calc(100% - ' + String(this.padding) + 'px * 2)'
Chrome会给我以下错误消息height: 'calc(100% - ' + String(this.padding) + 'px * 2)'
。
[ProjectContainer.js:15] Uncaught TypeError: Cannot read property 'padding' of undefined
[ProjectContainer.js:15]未捕获的TypeError:无法读取未定义的属性“ padding”
var styles = {
root: {
display: 'flex',
flexWrap: 'wrap',
textAlign: 'left',
padding: 10,
height: 'calc(100% - ' + String(this.padding) + 'px * 2)',
width: 'calc(100% - 10px * 2)',
overflowY: 'scroll'
}
}
However, if I make it as a function, no error message is displayed but the css seems not working. 但是,如果我将其作为函数使用,则不会显示任何错误消息,但CSS似乎无法正常工作。 This may because the inline-style system does not call it as a function but variable.
这可能是因为内联样式系统不是将其称为函数而是变量。
var styles = {
root: {
display: 'flex',
flexWrap: 'wrap',
textAlign: 'left',
padding: 10,
height: function() {return 'calc(100% - ' + String(this.padding) + 'px * 2)'},
width: 'calc(100% - 10px * 2)',
overflowY: 'scroll'
}
}
What is the best way to make the height to be 'calc(100% - 10px * 2)'
? 将高度设为
'calc(100% - 10px * 2)'
的最佳方法是什么?
As noted in the comments, you can't use this
to refer to your object literal as you are defining it. 如注释中所述,您不能在定义对象时使用
this
来引用对象文字。 Instead it will refer to the global this
, or the this
in the current scope of the code. 相反,它将在代码的当前范围内引用全局
this
或this
。 Basically you cannot use the properties of an object literal while you are still defining it, you will need to do so afterwards: 基本上,当您仍在定义对象文字时,不能使用它的属性,之后需要使用它:
var styles = { root: { display: 'flex', flexWrap: 'wrap', textAlign: 'left', padding: 10, height: '', // Optional width: 'calc(100% - 10px * 2)', overflowY: 'scroll' } } styles.root.height = 'calc(100% - ' + String(styles.root.padding) + 'px * 2)'; console.log(styles);
You can try this 你可以试试这个
var styles = {
root: {
display: 'flex',
flexWrap: 'wrap',
textAlign: 'left',
padding: 10,
width: 'calc(100% - 10px * 2)',
overflowY: 'scroll'
},
init: function() {
this.root.height = 'calc(100% - ' + String(this.root.padding) + 'px * 2)';
delete this.init;
return this;
},
}.init()
You could do: 您可以这样做:
var styles = (function() {
var padding = '10';
return {
root: {
display: 'flex',
flexWrap: 'wrap',
textAlign: 'left',
padding: +padding,
height: 'calc(100% - ' + padding + 'px * 2)',
width: 'calc(100% - 10px * 2)',
overflowY: 'scroll'
}
})();
This makes use of a couple fun Javascript techniques such as: 这利用了一些有趣的Javascript技术,例如:
'10'
into a number '10'
强制转换为数字 This has the advantage of not only being done in a single expression, but also being easier to read and understand. 这样的优点不仅在于可以在单个表达式中完成,而且还易于阅读和理解。
AND you could modify it so that the padding value could be passed in from the outside with almost no new work: 并且您可以对其进行修改,以便几乎无需任何新工作就可以从外部传递padding值:
var styles = (function(padding) {
var padding = padding ? padding : '10';
return {
root: {
display: 'flex',
flexWrap: 'wrap',
textAlign: 'left',
padding: +padding,
height: 'calc(100% - ' + padding + 'px * 2)',
width: 'calc(100% - 10px * 2)',
overflowY: 'scroll'
}
})(padding);
You cannot use this
inside an object literal to reference a properly during declaration. 您不能在对象文字中使用
this
参数在声明期间正确引用。 this
in JavaScript is used for function scope. this
在JavaScript中用于功能范围。
The example below shows a possible solution to your problem. 下面的示例显示了可能解决您的问题的方法。 You basically define
padding
in a function scope and retrieve it within your object literal declaration. 您基本上是在函数范围内定义
padding
,并在对象文字声明中对其进行检索。
In most cases, the value of this is determined by how a function is called.
在大多数情况下,其值取决于函数的调用方式。 It can't be set by assignment during execution, and it may be different each time the function is called.
在执行过程中不能通过赋值来设置它,并且每次调用该函数时可能会有所不同。 More info on this here .
有关此的更多信息 。
function foo() { this.padding = 10; // declare it only once var styles = { root: { display: 'flex', flexWrap: 'wrap', textAlign: 'left', padding: this.padding, height: 'calc(100% - ' + String(this.padding) + 'px * 2)', width: 'calc(100% - 10px * 2)', overflowY: 'scroll' } }; console.log(styles.root); }; foo();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.