Git Product home page Git Product logo

Comments (3)

CwRv07 avatar CwRv07 commented on August 15, 2024
Array.prototype._forEach = function (cb, thisBinding = globalThis) {
  // 排除回调非函数情况
  if (typeof cb !== "function") {
    throw new TypeError(`${cb} is not a function`);
  }
  // 排除this为非可迭代对象情况
  if (this == null || typeof this[Symbol.iterator] !== "function") {
    throw new TypeError(`${this} is not a iterable`);
  }
  // 将可迭代对象转换成数组
  const array = [...this];
  // 执行遍历并回调
  for (let i = 0; i < array.length; i++) {
    cb.call(thisBinding, array[i], i, this);
  }
};

from js-challenges.

hannah-bingo avatar hannah-bingo commented on August 15, 2024

forEach和map类似,但是forEach不返回结果

Array.prototype._forEach = function(callback, objThis) {
	if(typeof callback !== "function") {
		throw new TypeError("callback type error!")
	}
	for(let i = 0; i < this.length; i++) {
		callback.call(objThis, this[i], i, this);
	}	
}

from js-challenges.

kangkang123269 avatar kangkang123269 commented on August 15, 2024
Array.prototype.myForEach = function(callback, thisArg) {
  if (this == null) {
    throw new TypeError("Cannot read property 'forEach' of null or undefined");
  }
  if (typeof callbackFn !== 'function') {
    throw new TypeError(callbackFn + ' is not a function');
  }
  for (let i = 0; i < this.length; i++) {
    callback.call(thisArg, this[i], i, this);
  }
};

from js-challenges.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.