美文网首页
用原型实现排序

用原型实现排序

作者: Hello杨先生 | 来源:发表于2019-07-17 14:52 被阅读0次
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
    </head>
    
    <body>
        <script>
            var arr = [2, 34, 5, 33, 45]
            var temp;
            arr.prototype = Array.prototype.for;
            Array.prototype.for = function () {
                for (var i = 0; i < arr.length; i++) {
                    for (var j = i + 1; j < arr.length; j++) {
                        if (arr[i] > arr[j]) {
                            temp = arr[i];
                            arr[i] = arr[j];
                            arr[j] = temp;
                        }
                    }
                }
            }
            arr.for();
            alert(arr)
        </script>
        <!-- <script>
            function Arr() {}
            Arr.prototype.newarr = [2, 1, 8, 56, 24, 45, 32];
            Arr.prototype.forSort = function () {
                for (var i = 0; i < arr.newarr.length; i++) {
                    for (var j = i + 1; j < arr.newarr.length; j++) {
                        if (arr.newarr[i] > arr.newarr[j]) {
                            var temp = arr.newarr[i];
                            arr.newarr[i] = arr.newarr[j];
                            arr.newarr[j] = temp;
                        }
                    }
                }
                document.write(arr.newarr);
            };
            var arr = new Arr();
            arr.forSort();
        </script> -->
    </body>
    
    </html>
    

    相关文章

      网友评论

          本文标题:用原型实现排序

          本文链接:https://www.haomeiwen.com/subject/woiqlctx.html