> 웹 프론트엔드 > JS 튜토리얼 > 함수 프로토타입 - JavaScript 과제

함수 프로토타입 - JavaScript 과제

Mary-Kate Olsen
풀어 주다: 2024-11-02 04:16:30
원래의
1038명이 탐색했습니다.

Function prototype - JavaScript Challenges

이 게시물의 모든 코드는 repo Github에서 확인하실 수 있습니다.


함수 프로토타입 관련 과제


Function.prototype.call()

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

/**

 * @param {any} thisArg

 * @param {...*} argArray

 * @return {any}

 */

 

Function.prototype.myCall = function (thisArg, ...argArray) {

  const sym = Symbol();

  const wrapperObj = Object(thisArg);

 

  Object.defineProperty(wrapperObj, sym, {

    enumerable: false,

    value: this,

  });

 

  return wrapperObj[sym](...argArray);

};

 

// Usage example

function multiplyAge(multiplier = 1) {

  return this.age * multiplier;

}

 

const mary = {

  age: 21,

};

 

const john = {

  age: 42,

};

 

console.log(multiplyAge.myCall(mary)); // 21

console.log(multiplyAge.myCall(john, 2)); // 84

로그인 후 복사

함수.프로토타입.적용()

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

/**

 * @param thisArg The object to be used as the this object.

 * @param argArray A set of arguments to be passed to the function.

 * @return {any}

 */

 

Function.prototype.myApply = function (thisArg, argArray = []) {

  const sym = Symbol();

  const wrapperObj = Object(thisArg);

 

  Object.defineProperty(wrapperObj, sym, {

    enumerable: false,

    value: this,

  });

 

  return wrapperObj[sym](...argArray);

};

 

// Usage example

function multiplyAge(multiplier = 1) {

  return this.age * multiplier;

}

 

const mary = {

  age: 21,

};

 

const john = {

  age: 42,

};

 

console.log(multiplyAge.myApply(mary)); // 21

console.log(multiplyAge.myApply(john, [2])); // 84

로그인 후 복사

함수.프로토타입.바인드()

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

/**

 * @param {any} thisArg

 * @param {...*} argArray

 * @return {Function}

 */

 

Function.prototype.myBind = function (thisArg, ...argArray) {

  const sym = Symbol();

  const wrapperObj = Object(thisArg);

 

  Object.defineProperty(wrapperObj, sym, {

    enumerable: false,

    value: this,

  });

 

  return function (...args) {

    return wrapperObj[sym](...argArray, ...args);

  };

};

 

// Usage example

const john = {

  age: 42,

  getAge: function () {

    return this.age;

  },

};

 

const unboundGetAge = john.getAge;

console.log(unboundGetAge()); // undefined

 

const boundGetAge = john.getAge.myBind(john);

console.log(boundGetAge()); // 42

 

const jack = {

  age: 21,

  getAge: function () {

    return this.age;

  },

};

 

// For multiple `.bind()` chaining, only the first one would work

const boundJohnGetAge = john.getAge.myBind(john).myBind(jack);

console.log(boundGetAge()); // 42

로그인 후 복사

참조

  • 그레이트프론트엔드
  • 61. 나만의 Function.prototype.call 만들기
  • Function.prototype.call()
  • Function.prototype.apply()
  • Function.prototype.bind()

위 내용은 함수 프로토타입 - JavaScript 과제의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿