zoukankan      html  css  js  c++  java
  • es5中foreach的用法

    HTML代码:
    <p id="result"></p>
    JS代码:
    var eleResult = document.getElementById("result");
    if (!window.console) {
        window.console = {};
    }
    console.log = function(result) {
        var text = document.createTextNode(result), 
         br = document.createElement("br"); eleResult.appendChild(text); eleResult.appendChild(br); }; //处理兼容的问题 if (typeof Array.prototype.forEach != "function") { Array.prototype.forEach = function (fn, context) { for (var k = 0, length = this.length; k < length; k++) { if (typeof fn === "function" && Object.prototype.hasOwnProperty.call(this, k)) { fn.call(context, this[k], k, this); } } }; } var database = { users: ["张含韵", "江一燕", "李小璐"], sendEmail: function (user) { if (this.isValidUser(user)) { console.log("你好," + user); } else { console.log("抱歉,"+ user +",你不是本家人"); } }, isValidUser: function (user) { return /^张/.test(user); } }; // 给每个人法邮件 database.users.forEach( // database.users中人遍历 database.sendEmail, // 发送邮件 database // 使用database代替上面database.sendEmail方法中的上下文this );
  • 相关阅读:
    LeetCode 121. Best Time to Buy and Sell Stock
    LeetCode 221. Maximal Square
    LeetCode 152. Maximum Product Subarray
    LeetCode 53. Maximum Subarray
    LeetCode 91. Decode Ways
    LeetCode 64. Minimum Path Sum
    LeetCode 264. Ugly Number II
    LeetCode 263. Ugly Number
    LeetCode 50. Pow(x, n)
    LeetCode 279. Perfect Squares
  • 原文地址:https://www.cnblogs.com/fireporsche/p/6364561.html
Copyright © 2011-2022 走看看