简体   繁体   English

Modernizr和Video.h264

[英]Modernizr and Video.h264

I am looking at how Modernizr detects support for Video and H.264 , but it does not make any sense to me. 我正在研究Modernizr如何检测到对Video和H.264的支持,但对我来说没有任何意义。 Isn't bool a primitive boolean? bool不是原始布尔值吗? why does it become a Boolean object? 为什么它变成布尔对象? Why does bool.h264 magically start making any sense at all? 为什么bool.h264神奇地开始变得毫无意义? thanks 谢谢

tests['video'] = function() {
        var elem = document.createElement('video'),
            bool = false;

        // IE9 Running on Windows Server SKU can cause an exception to be thrown, bug #224
        try {
            if ( bool = !!elem.canPlayType ) {
                bool      = new Boolean(bool);
                bool.ogg  = elem.canPlayType('video/ogg; codecs="theora"')      .replace(/^no$/,'');

                // Without QuickTime, this value will be `undefined`. github.com/Modernizr/Modernizr/issues/546
                bool.h264 = elem.canPlayType('video/mp4; codecs="avc1.42E01E"') .replace(/^no$/,'');

                bool.webm = elem.canPlayType('video/webm; codecs="vp8, vorbis"').replace(/^no$/,'');
            }

        } catch(e) { }

        return bool;
    };

Initially bool is indeed a primitive boolean, which cannot have properties added to it. 最初, bool确实是一个原始的布尔值,不能向其添加属性。 Following the if condition, the value of bool gets overwritten via new Boolean(bool) . 遵循if条件后, bool的值将通过new Boolean(bool)覆盖。 In Javascript, calling new on a function, creates an empty Object, and uses the function as a constructor for said object. 在Javascript中,在函数上调用new会创建一个空对象,并将该函数用作该对象的构造函数。 In the case of the Boolean() constructor, the only notable addition to the object, is a valueOf() function which returns the original primitive value. 对于Boolean()构造函数,该对象唯一值得注意的增加是valueOf()函数,该函数返回原始原始值。 Otherwise, you now have a normal Object which can have arbitrary properties added to it. 否则,您现在将拥有一个普通对象,该对象可以添加任意属性。

See here and here . 看到这里这里

I actually wrote a majority of that test. 我实际上写了该测试的大部分内容。

That is just the format that Modernizr has followed for detects with sub values. 那只是Modernizr遵循的用于检测子值的格式。 In JavaScript, everything is an object, and new Boolean is as well. 在JavaScript中,一切都是对象,新的Boolean也是如此。

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

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