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 );
时间: 2024-12-14 09:02:53