码迷,mamicode.com
首页 > Web开发 > 详细

js原生方法map实现

时间:2020-01-19 11:07:10      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:res   log   lan   scale   prototype   ++   meta   isa   auth   

<!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">
    <meta name="author" content="杨欣">
    <title>map</title>
</head>

<body>

    <script>
        Array.prototype.my_map = function (callback) {
            if (!Array.isArray(this) || !this.length || typeof callback !== 'function') {
                return []
            } else {
                let result = [];
                for (let index = 0; index < this.length; index++) {
                    const element = this[index];
                    result.push(callback(element, index, this))
                }
                return result
            }
        }

        let arr = [1, 2, 3, 4, 5]
        let res = arr.my_map((ele, i) => {
            return ele + 10
        })
        console.log(res)
    </script>
</body>

</html>

js原生方法map实现

标签:res   log   lan   scale   prototype   ++   meta   isa   auth   

原文地址:https://www.cnblogs.com/samsara-yx/p/12212799.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!