replace(循环内)回调函数

时间:2022-12-08 19:57:16

I have this piece of code and ran it through JSLint to make sure everything is according to best practices. It's basically a custom feature detection.

我有这段代码,并通过JSLint运行它,以确保一切都符合最佳实践。这基本上是一个自定义特征检测。

(function() {
var vendorArray = ['', '-webkit-'],
    vendorIndex = vendorArray.length,
    mergedProperty,
    validProperty,
    activePrefix,
    detectorElement = document.createElement('detector'),
    detectorStyle = detectorElement.style,
    prefixRegex = new RegExp('%prfx%', 'g'),
    camelRegex = new RegExp('\-([a-z])', 'g'),
    propertyArray = [
        ['backgroundImage', 'background-image: %prfx%linear-gradient(rgba(0, 0, 0, 0), rgba(0, 0, 0, 1)), %prfx%linear-gradient(rgba(0, 0, 0, 0.5) 1%, rgba(0, 0, 0, 0.5) 99%)'],
        ['boxShadow', 'box-shadow: 1px 1px 1px 1px rgba(0, 0, 0, 0.5), inset 1px 1px 1px 1px rgba(0, 0, 0, 1)'],
        ['transition', '%prfx%transition: all 1s'],
        ['animation', '%prfx%animation: example 1s'],
        ['transform', '%prfx%transform: translate3d(1px, 1px, 1px)']
    ];

// property ist z.B. propertyArray[0] also das erste Property im Array; mit prefix kann man angeben ob das js Attribut ein Vendor-Prefix benötigt - 0 oder 1
function vendorCheck(property, prefix) {
    // Zur Sicherheit löschen wir das gesamte style-Attribut zu Beginn
    if (detectorElement.getAttribute('style')) {
        detectorElement.removeAttribute('style');
    }
    while (vendorIndex--) {
        // activePrefix cached den Index bzw. das Vendor-Prefix der Schleife z.b. -webkit-
        activePrefix = vendorArray[vendorIndex];
        // Das detector Element bekommt den Stil gesetzt >> property[1] = propertyArray[0][1] (propertyArray[0] wurde in die Funktion übergeben) >> im Stil wird der Platzhalter %prfx% mit dem jeweiligen aktiven Vendor-Prefix ersetzt
        detectorStyle.cssText = property[1].replace(prefixRegex, activePrefix);
        // Überprüfen ob das Attribut z.b. transition einen Vendor-Prefix erhalten soll, z.B. WebkitTransition
        if (prefix === 1) {
            // Aus dem String mergedProperty z.b. -webkit-transition wird -w und -t extrahiert und in Großbuchstaben zurückgegeben
            mergedProperty = activePrefix + property[0];
            validProperty = mergedProperty.replace(camelRegex, function(match, p1) {
                return p1.toUpperCase();
            });
        }
        // Wird prefix nicht übergeben, ist das Attribut gleich dem Attribut aus propertyArray
        else {
            validProperty = property[0];
        }
        // Wenn das detector Element der angewendeten Stil behalten hat, also das Attribut validProperty = property[0] = propertyArray[0][0] true ist, wird z.b. das Attribut WebkitTransition zurückgegeben und die Schleife beendet
        if (detectorStyle[validProperty]) {
            return validProperty;
        }
    }
};

document.getElementsByTagName('body')[0].innerHTML = detectorStyle[vendorCheck(propertyArray[4], 1)];

}());

However JSLint tells me that this part has some issues, since I'm declaring a function within a loop:

但是JSLint告诉我这部分有一些问题,因为我在循环中声明了一个函数:

validProperty = mergedProperty.replace(camelRegex, function(match, p1) {
                return p1.toUpperCase();
            });

Is there a way to have a named callback function that is outside the loop?

是否有一种方法可以将一个命名回调函数放在循环之外?

Find a Codepen here: http://codepen.io/lieferant/pen/kXJLjJ

在这里找到一个代码页:http://codepen.io/lieferant/pen/kXJLjJ

1 个解决方案

#1


2  

Is there a way to have a named callback function that is outside the loop?

是否有一种方法可以将一个命名回调函数放在循环之外?

Yes, just declare it within your function, and call it:

是的,只要在函数中声明它,并调用它:

function vendorCheck(property, prefix) {

    // ...

    while (/*...*/) {
        validProperty = mergedProperty.replace(capitalizeFirstCaptureGroup);
    }

    // ...

    function capitalizeFirstCaptureGroup(match, p1) {
        return p1.toUpperCase();
    }

    // ...
}

It can be at the top or bottom. It should be at the top level of the function, not within any control blocks. (As of ES2015, it's valid if it's inside a control block, but the rules are...complicated.)

它可以在顶部或底部。它应该位于函数的顶层,而不是在任何控制块中。(从ES2015年开始,如果它位于控制块内,是有效的,但规则……很复杂。)

Although actually, that one is general enough you might put it in a general utilities area and use it more broadly than just within vendorCheck.

尽管实际上,这一项是足够通用的,你可以把它放在一个通用的公用事业领域,并且比在vendorCheck中更广泛地使用它。

#1


2  

Is there a way to have a named callback function that is outside the loop?

是否有一种方法可以将一个命名回调函数放在循环之外?

Yes, just declare it within your function, and call it:

是的,只要在函数中声明它,并调用它:

function vendorCheck(property, prefix) {

    // ...

    while (/*...*/) {
        validProperty = mergedProperty.replace(capitalizeFirstCaptureGroup);
    }

    // ...

    function capitalizeFirstCaptureGroup(match, p1) {
        return p1.toUpperCase();
    }

    // ...
}

It can be at the top or bottom. It should be at the top level of the function, not within any control blocks. (As of ES2015, it's valid if it's inside a control block, but the rules are...complicated.)

它可以在顶部或底部。它应该位于函数的顶层,而不是在任何控制块中。(从ES2015年开始,如果它位于控制块内,是有效的,但规则……很复杂。)

Although actually, that one is general enough you might put it in a general utilities area and use it more broadly than just within vendorCheck.

尽管实际上,这一项是足够通用的,你可以把它放在一个通用的公用事业领域,并且比在vendorCheck中更广泛地使用它。