본문 바로가기
개념/혼자 공부하는 Javascript

method) prototype method

by kiseno 2025. 1. 15.
728x90
반응형
SMALL
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>
        //number
        Number.prototype.power = function (n=2) {
            return this.valueOf() ** n
        }

        const a =12
        console.log('a.power() : ',a.power())
        console.log('a.power(3) : ', a.power(3))
        console.log('a.power(4) : ', a.power(4))

        //string
        String.prototype.contain = function(data){
            return this.indexOf(data) >= 0
        }

        Array.prototype.contain = function (data) {
            return this.indexOf(data) >= 0
        }

        const a = 'hello'
        console.log('hi in hello', a.contain('hi'))
        console.log('not in hello', a.contain('not'))

        const b = [273,524,352,214,249]
        console.log('273 in [273,524,352,214,249] : ', b.contain(273))
        console.log('0 in [273,524,352,214,249] : ', b.contain(0))
    </script>
</head>
<body>

</body>
</html>

### Extending `Number.prototype`
- A method `power` is added to `Number.prototype`, enabling any number to calculate its power. By default, it calculates the square (`n=2`), but it can take any exponent `n`.
- Examples provided (`a.power()`, `a.power(3)`, `a.power(4)`) show using this method to compute the square, cube, and fourth power of a number `12`.

### Extending `String.prototype` and `Array.prototype`
- A method `contain` is added to both `String.prototype` and `Array.prototype`. This method checks if a string contains a given substring or an array contains a given element, respectively, returning `true` or `false`.
- For strings, `a.contain('hi')` checks if the string `"hello"` contains the substring `"hi"`.
- For arrays, `b.contain(273)` checks if the array `[273,524,352,214,249]` contains the number `273`.

### Issue in the Script
- The constant `a` is declared twice: first as a number (`const a = 12`), then as a string (`const a = 'hello'`). This will cause a syntax error because `const` declarations do not allow a variable with the same name to be declared more than once in the same scope.

### Solution
To fix the script and avoid the `SyntaxError` due to the redeclaration of `a`, you could rename one of the constants, for example:

```

const numA = 12;
console.log('numA.power() : ', numA.power());
console.log('numA.power(3) : ', numA.power(3));
console.log('numA.power(4) : ', numA.power(4));

const strA = 'hello';
console.log("hi in hello", strA.contain('hi'));
console.log("not in hello", strA.contain('not'));


```

### Important Note on Extending Built-in Prototypes
While JavaScript allows extending built-in prototypes, it's generally advised against in practice. Modifying prototypes of built-in objects can lead to conflicts, especially with third-party libraries, future features that might use the same method names, or other code in the application expecting the original behavior of these objects. It's usually better to create utility functions or use classes/objects that encapsulate the desired functionality.

728x90
반응형
LIST

'개념 > 혼자 공부하는 Javascript' 카테고리의 다른 글

method) map() method  (0) 2025.01.17
method) Math.random() method  (0) 2025.01.16
method) querySelector()  (0) 2025.01.14
method) querySelectorAll() method  (0) 2025.01.13
method) filter()  (0) 2025.01.12