简体   繁体   English

JavaScript字符串内插

[英]Javascript string interpolation

I have a Galleria (galleria.io) that works when the code looks like this: 我有一个Galleria(galleria.io),其代码如下所示:

Galleria.run('.galleria', {
  flickr: 'set:72157640607666844', }

But when I try to set the set number as a variable, it breaks with a "Fatal error: 1 fail: Photoset not found" 但是,当我尝试将设置号设置为变量时,它以“致命错误:1失败:找不到照片集”而中断

var fakeVar = 72157640607666844
  Galleria.run('.galleria', {
  flickr: 'set:' + fakeVar, }

Any thoughts on how to get this to work? 关于如何使它起作用的任何想法?

FYI: I looked at console.log('set:' + fakeVar) , and it certainly is returning set:72157640607666844. 仅供参考:我查看了console.log('set:' + fakeVar) ,它肯定会返回set:72157640607666844。 Initially, I thought it was because it was returning the set without quotes, but I know this is not the case, because if I do " 'set:"+ fakeVar + " ' " , I get a no method error for 'set. 最初,我以为是因为它返回的是不带引号的集合,但我知道不是这样,因为如果我执行" 'set:"+ fakeVar + " ' " ,我会得到'set的no方法错误。

Also if it's helpful, this is the Galleria program code that defines the set method: 同样,如果有帮助的话,这是定义set方法的Galleria程序代码:

set: function( photoset_id, callback ) {
    return this._find({
        photoset_id: photoset_id,
        method: 'flickr.photosets.getPhotos'
    }, callback);
},

Try this in your JavaScript console: 在您的JavaScript控制台中尝试以下操作:

alert('set:' + 72157640607666844); alerts "set:72157640607666850" for me
parseInt("72157640607666844") // returns 72157640607666850 for me

JavaScript can't deal with numbers this large. JavaScript无法处理这么大的数字。 It uses double-precision floating-point internally to represent numbers, and 72157640607666850 is as closed to 72157640607666844 as JavaScript can manage. 它内部使用双精度浮点表示数字,并且72157640607666850与JavaScript可以管理的72157640607666844保持接近。 You'll need to leave it in string format or use a library like BigNumber to deal with this. 您需要将其保留为字符串格式或使用像BigNumber这样的库来处理。

Here's more information on representing Integers in a variety of languages. 这是有关以多种语言表示整数更多信息

72157640607666844 is too significant of a Number for JavaScript to store completely: 72157640607666844Number太重要了,无法完全存储JavaScript:

console.log(72157640607666844);
//          72157640607666850

To avoid issues with precision, you can wrap the literal in quotes so it's a String instead: 为了避免精度问题,您可以将文字包装在引号中,因此使用String代替:

var fakeVar = '72157640607666844';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM