Warning: preg_grep(): Compilation failed: quantifier does not follow a repeatable item at offset 27 in /var/www/tg-me/post.php on line 75
Frontend | Вопросы собесов | Telegram Webview: easy_javascript_ru/1615 -
Telegram Group & Telegram Channel
🤔 Как бы добавлял статический метод в prototype?

На самом деле, статические методы не добавляются в prototype, потому что они принадлежат самому классу, а не его экземплярам.
Но если ты хочешь имитировать статический метод в prototype, можно использовать функцию-конструктор и добавить метод вручную.

🚩Как работают статические методы? (`static`)

В классе статические методы объявляются с помощью static. Они не находятся в prototype, а принадлежат самому классу.
class User {
static sayHello() {
return "Привет!";
}
}

console.log(User.sayHello()); // "Привет!"
console.log(User.prototype.sayHello); // undefined (нет в prototype)


🚩Добавление "статического" метода в `prototype` (не совсем статический)

Если нужно, чтобы каждый объект имел доступ к "статическому" методу через prototype, можно сделать так
function User(name) {
this.name = name;
}

// Добавляем метод в prototype
User.prototype.sayHello = function () {
return "Привет!";
};

const user1 = new User("Иван");
console.log(user1.sayHello()); // "Привет!"


🚩Добавление метода напрямую в сам класс (имитация `static`)

Если хочется добавить метод на сам класс, а не в prototype, можно сделать так
function User(name) {
this.name = name;
}

// Добавляем метод прямо в функцию-конструктор
User.sayHello = function () {
return "Привет!";
};

console.log(User.sayHello()); // "Привет!"


Ставь 👍 и забирай 📚 Базу знаний
Please open Telegram to view this post
VIEW IN TELEGRAM



tg-me.com/easy_javascript_ru/1615
Create:
Last Update:

🤔 Как бы добавлял статический метод в prototype?

На самом деле, статические методы не добавляются в prototype, потому что они принадлежат самому классу, а не его экземплярам.
Но если ты хочешь имитировать статический метод в prototype, можно использовать функцию-конструктор и добавить метод вручную.

🚩Как работают статические методы? (`static`)

В классе статические методы объявляются с помощью static. Они не находятся в prototype, а принадлежат самому классу.

class User {
static sayHello() {
return "Привет!";
}
}

console.log(User.sayHello()); // "Привет!"
console.log(User.prototype.sayHello); // undefined (нет в prototype)


🚩Добавление "статического" метода в `prototype` (не совсем статический)

Если нужно, чтобы каждый объект имел доступ к "статическому" методу через prototype, можно сделать так
function User(name) {
this.name = name;
}

// Добавляем метод в prototype
User.prototype.sayHello = function () {
return "Привет!";
};

const user1 = new User("Иван");
console.log(user1.sayHello()); // "Привет!"


🚩Добавление метода напрямую в сам класс (имитация `static`)

Если хочется добавить метод на сам класс, а не в prototype, можно сделать так
function User(name) {
this.name = name;
}

// Добавляем метод прямо в функцию-конструктор
User.sayHello = function () {
return "Привет!";
};

console.log(User.sayHello()); // "Привет!"


Ставь 👍 и забирай 📚 Базу знаний

BY Frontend | Вопросы собесов


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/easy_javascript_ru/1615

View MORE
Open in Telegram


Frontend | Вопросы собесов Telegram | DID YOU KNOW?

Date: |

A project of our size needs at least a few hundred million dollars per year to keep going,” Mr. Durov wrote in his public channel on Telegram late last year. “While doing that, we will remain independent and stay true to our values, redefining how a tech company should operate.

How to Use Bitcoin?

n the U.S. people generally use Bitcoin as an alternative investment, helping diversify a portfolio apart from stocks and bonds. You can also use Bitcoin to make purchases, but the number of vendors that accept the cryptocurrency is still limited. Big companies that accept Bitcoin include Overstock, AT&T and Twitch. You may also find that some small local retailers or certain websites take Bitcoin, but you’ll have to do some digging. That said, PayPal has announced that it will enable cryptocurrency as a funding source for purchases this year, financing purchases by automatically converting crypto holdings to fiat currency for users. “They have 346 million users and they’re connected to 26 million merchants,” says Spencer Montgomery, founder of Uinta Crypto Consulting. “It’s huge.”

Frontend | Вопросы собесов from us


Telegram Frontend | Вопросы собесов
FROM USA