创建一个比这更有效的长字符串

create a really long string more efficiently than this

本文关键字:有效 字符串 一个 创建      更新时间:2023-09-26

所以,我对这个游戏很陌生,并且试图比现在更好地理解javaScript。我有这段代码,如果它太长而无法阅读,那么只需跳到底部的问题......

    function createCSSRule(selectorName, necessaryProperties){
    //add class to control all divs
    var propertyNameBases, propertyPrefixes, propertyValues, propertySuffixes;
    var cssString = selectorName + "{'n";
    for (var i9 = 0; i9 < necessaryProperties.length; ++i9){
        switch (selectorName){
            case "."+options.allPictures:
                switch(necessaryProperties[i9]){
                    case "position":
                        propertyNameBases = ["position"];
                        propertyPrefixes    = [""],
                        propertyValues      = ["absolute"],
                        propertySuffixes    = [""];
                        break;
                    case "height":
                        propertyNameBases = ["height"];
                        propertyPrefixes    = [""],
                        propertyValues      = ["100%"],
                        propertySuffixes    = [""];
                        break;
                    case "width":
                        propertyNameBases = ["width"];
                        propertyPrefixes    = [""],
                        propertyValues      = ["100%"],
                        propertySuffixes    = [""];
                        break;
                    case "background":
                        propertyNameBases = ["background"];
                        propertyPrefixes    = [""],
                        propertyValues      = ["scroll","#fff","50% 50%","no-repeat","cover"],
                        propertySuffixes    = ["-attachment","-color","-position","-repeat","-size"];
                        break;
                    case "transform":
                        propertyNameBases   = ["transform"],
                        propertyPrefixes    = ["", "-moz-", "-webkit-"],
                        propertyValues      = [options.threeDOrigin,options.threeDStyle,"translate3d("+options.translate3dpx+")"],
                        propertySuffixes    = ["-origin","-style",""];
                        break;
                    case "transition":
                        propertyNameBases = ["transition"],
                        propertyPrefixes    = ["", "-webkit-"],
                        propertyValues      = [options.transitionLength + "ms", options.transitionPath, "all"],
                        propertySuffixes    = ["-duration","-timing-function","-property"]; //-delay"];                 
                        break;
                    default:
                        console.log("missing");
                        propertyNameBases   = null;
                        propertyPrefixes    = null;
                        propertyValues      = null;
                        propertySuffixes    = null;
                        break;
                }
                break;
        case "."+options.currentPic:
            switch(necessaryProperties[i9]){
                    case "transform":
                        propertyNameBases   = ["transform"],
                        propertyPrefixes    = ["", "-moz-", "-webkit-"],
                        propertyValues      = [options.threeDOrigin,"translate3d(0px, 0px, 0px)"],
                        propertySuffixes    = ["-origin",""];
                        break;
                    default:
                        console.log("missing");
                        propertyNameBases   = null;
                        propertyPrefixes    = null;
                        propertyValues      = null;
                        propertySuffixes    = null;
                        break;
                }
                break;
        case "."+options.currentPic+"."+options.picAfterCurrent:
            switch(necessaryProperties[i9]){
                    case "transform":
                        propertyNameBases   = ["transform"],
                        propertyPrefixes    = ["", "-moz-", "-webkit-"],
                        propertyValues      = [options.threeDOrigin,"translate3d("+options.negativeTranslate3dpx+")"],
                        propertySuffixes    = ["-origin",""];
                        break;
                    default:
                        console.log("missing");
                        propertyNameBases   = null;
                        propertyPrefixes    = null;
                        propertyValues      = null;
                        propertySuffixes    = null;
                        break;
                }
                break;
            default:
                console.log("wait a second");
                break;
        }
        //name the selector
        //iterate through properties
        for (i10 = 0; i10 < propertyNameBases.length; i10++){
            //iterate through suffixes and value pairs
            for (var i11 = 0; i11 < propertyValues.length; i11++){
                //iterate through prefixes
                if(propertyValues !== false){
                    for (var i12 = 0; i12 < propertyPrefixes.length; i12++){
                        cssString = cssString+" "+propertyPrefixes[i12]+propertyNameBases[i10]+propertySuffixes[i11]+": "+propertyValues[i11]+";'n"
                    }
                }
            }
        }
    }
var forAllPictures = ["position","height","width","background","transition","transform"];   
var forCurrentPic = ["transform"];
var forpicAfterCurrent = ["transform"];
createCSSRule("."+options.allPictures, forAllPictures);
createCSSRule("."+options.currentPic, forCurrentPic);
createCSSRule("."+options.currentPic+"."+options.picAfterCurrent, forpicAfterCurrent);
基本上,将

要发生的事情是我将一个字符串(在变量组合中)传递给第一个参数,将数组传递给第二个参数。第一个参数充当我的类名,第二个参数充当我的必要 css 属性数组。我在下面包含了输出,以便您可以简单了解我要做什么。if 语句中的每个数组都由每个 for 循环中的 i 使用来输出字符串。

每个 switch 语句设置一个特定的变量,然后 3 个 for 循环接管连接一个很长的字符串,这恰好是下面的 css

.slideShowPics{
    position: absolute;
    height: 100%;
    width: 100%;
    background-attachment: scroll;
    background-color: #fff;
    background-position: 50% 50%;
    background-repeat: no-repeat;
    background-size: cover;
    transition-duration: 5000ms;
    -webkit-transition-duration: 5000ms;
    transition-timing-function: ease-in;
    -webkit-transition-timing-function: ease-in;
    transition-property: all;
    -webkit-transition-property: all;
    transform-origin: 0% 0%;
    -moz-transform-origin: 0% 0%;
    -webkit-transform-origin: 0% 0%;
    transform-style: flat;
    -moz-transform-style: flat;
    -webkit-transform-style: flat;
    transform: translate3d(-640px, 0px, 0px);
    -moz-transform: translate3d(-640px, 0px, 0px);
    -webkit-transform: translate3d(-640px, 0px, 0px);
}
.currentSlideShowPic{
    transform-origin: 0% 0%;
    -moz-transform-origin: 0% 0%;
    -webkit-transform-origin: 0% 0%;
    transform: translate3d(0px, 0px, 0px);
    -moz-transform: translate3d(0px, 0px, 0px);
    -webkit-transform: translate3d(0px, 0px, 0px);
}
.currentSlideShowPic.movingOut{
    transform-origin: 0% 0%;
    -moz-transform-origin: 0% 0%;
    -webkit-transform-origin: 0% 0%;
    transform: translate3d(640px, 0px, 0px);
    -moz-transform: translate3d(640px, 0px, 0px);
    -webkit-transform: translate3d(640px, 0px, 0px);
}

我希望有人提出一种更简单的方法。

我觉得我没有正确使用这种语言。如果有人比我目前使用的有更好的想法,我很想听听。

就像我说的,我还在学习。

我觉得我应该能够用一个物体来做到这一点,我只是不知道当涉及到物体时我在做什么。如果有人有任何用干净的日常白话写的文章,或者至少一些非常好的例子,我将不胜感激,否则你自己的例子/解释将不胜感激。当然,如果我能够用一个对象做到这一点......

抱歉,我真的无法理解结果应该是什么样子,但这也表明......不要使用switch构造。当然,每个规则都有例外,但实际上 JavaScript 中存在switch原因,它有特殊的中缀运算符来操作 32 位整数位(甚至没有 32 位整数)。也就是说,这是 C 的黑暗遗产,这些操作非常有意义。JavaScript 被制作成类似于 C 是因为营销方面的考虑。开发人员被认为喜欢C,并且使语言类似于C会使其更受欢迎。

为什么不呢?

因为switch经常不会让你编写无法重用的重复代码。以你的这段代码为例:

case "height":
    propertyNameBases = ["height"];
    propertyPrefixes    = [""],
    propertyValues      = ["100%"],
    propertySuffixes    = [""];
    break;
case "width":
    propertyNameBases = ["width"];
    propertyPrefixes    = [""],
    propertyValues      = ["100%"],
    propertySuffixes    = [""];
    break;

它在第二个块中重复第一个块的 90%。但是,如果您需要更多这样的内容,您可以重复使用它吗?-不。

然后呢?

使用多态性。它在JavaScript中很便宜,很容易编写。下面是如何减少上述冗长程度的示例:

function rules() {
    function propertyTemplate(property) {
        return function (template, defalutValue) {
            return function (value) {
                return template.replace("%v", value || defalutValue);
            };
        }("%p:%v".replace("%p", property));
    }
    return {
        width: propertyTemplate("width", "100%"),
        height: propertyTemplate("height", "200%")
    };
}
// examples:
console.log(rules().width());
// width:100%
var text = [], ruleset = rules();
for (var rule in ruleset)
    text.push(ruleset[rule]((Math.random() * 100) | 0) + "%");
console.log(text.join(";"));
// width:47%;height:65%

当然,这只是一个例子,这取决于你让它做你想做的事。

听起来你可能正在重新发明轮子 - 为什么不使用jQuery或zeptojs,然后做

$('your css selector here').css({property:value, property:value...})

已经有一个JavaScript库可以完成你想要完成的事情。 http://lesscss.org/

就个人而言,我更喜欢sass-lang(不是javascript)http://sass-lang.com/

至于对象,那么它可以像这样简单:

// An object with css properties
var css = {
    background: "#FF0099",
    position: "absolute",
    height: "100%",
    width: "100%"
    //etc...
};
// You could also write it as such:
var css = {};
css["background"] = "#FF0099";
css["position"]   = "absolute";
css["height"]     = "100%";
css["width"]      = "100%";
// Parse the object into valid css using a string and a for in loop
var style = '.myClass{';
for (var i in css) {
    style += i + ':' + css[i] + ';';
}
style += '}';