Ключевое слово "this" в деталях
Ключевое слово this в javascript работает своеобразно, не так, как в других языках.
В отличие от PHP, Java, C++ и т.п, значение this в javascript не привязывается статически ни к какому объекту, а зависит от контекста вызова.
Разберем все 4 возможных случая.
Если функция вызывается через new как конструктор объекта, то this ставится на создаваемый объект:
function Animal(name) {
this.name = name
}
В результате получается объект, инициализированный конструктором, с заполненным свойством name .
Если функция запущена как свойство объекта, то в this будет ссылка на этот объект.
obj.func(параметры)
// или
obj['func'](параметры)
При этом совершенно неважно, откуда эта функция взялась. Важно лишь, какой перед func стоит объект.
Создадим два любых объекта:
vasya = {
name: "Василий"
}
dima = {
name: "Дмитрий"
}
Определим никак не связанную с ними функцию say :
say = function() {
alert("Привет, я "+this.name)
}
Присвоим функцию свойству sayHi для обоих объектов:
vasya.sayHi = say
dima.sayHi = say
И теперь тестовый запуск:
vasya.sayHi() // => "Привет, я Василий"
dima.sayHi() // => "Привет, я Дмитрий"
// или так
dima['sayHi']()
В обоих случаях запускается одна и та же функция say , которая получает в this объект до точки.
Функцию можно вызвать, используя методы call и apply .
func.apply(obj, [параметры])
func.call(obj, параметр)
При таком вызове this будет установлен в obj .
Оба метода работают одинаково, но для call аргументы подставляются в вызов, а для apply - передается массив.
function sum(a,b) {
this.c = a + b
}
var obj = {}
sum.call(obj, 1,2)
// или
sum.apply(obj, [1,2])
alert(obj.c) // => 3
Вызов через apply удобен тем, что можно самостоятельно сформировать массив аргументов или модифицировать существующий.
say()
При таком вызове this ставится равным глобальному объекту (в браузере это window ). Обычно, если функция использует this , то она не подразумевает запуск в таком виде, так что в 90% случаев такое значение this - результат ошибки в программе, глюк.
Если кратко срезюмировать все вышесказанное, то получится такая вот шпаргалка:
|
Very informative article, this is a very beautiful piece of work, thank you. If you are interested, we just have a site that provides the most popular online games in https://postermarketplace.com/
What a perfect article, very well organized and I get important points here, very high quality, thank you. Here we also write interesting things about football, such as news about players, matches, the point is we provide complete information related to football. If you are interested, you can continue to see the latest information, just by clicking once on the following link https://goalbet1x2.com/
I really like the latest useful information, that's why I keep going around to get new things, here I get something very interesting thank you. If you are interested we have also written the latest things about soccer, which is ready to surprise you with news and information that is very much awaited. If you are interested you can go directly to the https://footballreview.net/
线上娱乐城|
澳門賭場營業時間|
澳門賭場入場規定|
澳門賭場博彩遊戲種類|
10大澳門熱門賭場推薦|
威尼斯人娛樂場|
美高梅娛樂場|
新葡京娛樂場|
巴黎人娛樂場|
金沙娛樂場|
星際娛樂場|
永利娛樂場|
金砖赌场|
銀河娛樂場|
新濠影匯娛樂場|
倫敦人娛樂場|
银河星际娱乐场|
澳门银河娱乐场|
百利宫娱乐场|
太阳城app|
太阳城官网|
太阳城集团|
太阳城集团老板|
永利博彩官网|
澳门威尼斯人娱乐场|
澳门永利博彩官网|
澳门永利皇宫官网|
博彩公司排名|
永利皇宮官網|
永利皇宫酒店|
永利皇宫赌场|
澳门太阳城官网|
澳门太阳城赌场|
威尼斯人博彩|
美高梅赌场|
斗地主捕鱼|
葡京酒店夜总会|
澳门皇宫娱乐场|
利澳娱乐场|
巴比伦娱乐场|
金都娱乐场|
太阳城|
太阳城娱乐|
太阳城博彩|
太阳城网络赌博平台|
太阳城赌场|
博彩娱乐城|
太阳城娱乐集团|
金好运娱乐城|
马来西亚娱乐城|
线上博彩平台排名|
线上娱乐平台|
线上赌博平台|
真钱娱乐城|
线上赌博app|
马来西亚线上赌博合法吗|
新葡京娱乐场app|
新葡京博彩官网|
新葡京app|
澳门新葡京|
澳门新葡京官网|
新葡京真人在线ag真人娱乐官网|
在线21点|
真人视讯百家乐|
ag娱乐|
ag真人试玩|
ag百家乐官网|
百家乐网站|
ag真人平台|
百家乐app|
如何玩百家乐|
百家乐游戏|
百家乐玩法介绍|
百家乐规则表|
百家乐赔率|
百家乐平台|
百家乐在线|
百家乐路单下载|
百家乐单机版|
百家乐下载|
百家乐技巧|
金沙赌场|
金沙赌场网站|
金沙真人百家乐开户|
金沙娱乐城APP|
威尼斯赌场|
威尼斯人|
威尼斯人赌场|
威尼斯人在线开户|
威尼斯人app|
威尼斯人官网|
威尼斯人平台|
澳门威尼斯人赌场官网|
澳门威尼斯人网上赌场|
澳门威尼斯人赌场介绍|
澳门威尼斯人赌场攻略|
澳门威尼斯人app|
拉斯维加斯赌场|
太阳城娱乐官网|
太阳城娱乐城|
太阳城app下载|
太阳城集团官网|
太阳城娱乐开户|
太阳城娱乐代理|
线上太阳城|
线上太阳城开户|
线上太阳城代理|
线上太阳城官网|
线上太阳城娱乐|
太阳城澳门|
澳门太阳城|
澳门太阳城集团|
澳门太阳城集团APP|
澳门太阳城洗钱案|
澳门太阳城周焯华|
澳门太阳城娱乐|
澳门太阳城网址|
澳门太阳城博彩|
澳门太阳城网络赌博平台|
澳门太阳城娱乐城|
澳门太阳城APP|
澳门太阳城集团官网|
澳门太阳城开户|
澳门太阳城代理|
澳门太阳城娱乐开户|
澳门太阳城娱乐代理|
澳门太阳城网络博彩|
澳门太阳城在线开户|
澳门太阳城在线代理|
澳门太阳城在线娱乐|
澳门太阳城在线官网|
澳门太阳城赌博|
澳门太阳城在线赌博|
澳门太阳城赌博开户|
澳门太阳城赌博代理|
澳门太阳城博彩公司|
澳门太阳城AG|
澳门太阳城网址大全|
澳门太阳城如何开户|
澳门太阳城在线网址|
澳门太阳城娱乐网址|
澳门太阳城博彩网址|
澳门太阳城博彩开户|
澳门太阳城如何代理|
澳门太阳城哪里开户|
澳门太阳城官方网址|
菲律宾太阳城|
菲律宾太阳城集团|
菲律宾太阳城集团APP|
菲律宾太阳城洗钱案|
菲律宾太阳城周焯华|
菲律宾太阳城娱乐|
菲律宾太阳城赌场|
菲律宾太阳城官网|
菲律宾太阳城网址|
菲律宾太阳城博彩|
菲律宾太阳城网络赌博平台|
菲律宾太阳城娱乐城|
菲律宾太阳城APP|
菲律宾太阳城集团官网|
菲律宾太阳城开户|
菲律宾太阳城代理|
菲律宾太阳城娱乐开户|
菲律宾太阳城娱乐代理|
菲律宾太阳城网络博彩|
菲律宾太阳城在线开户|
菲律宾太阳城在线代理|
菲律宾太阳城在线娱乐|
菲律宾太阳城在线官网|
菲律宾太阳城赌博|
菲律宾太阳城在线赌博|
菲律宾太阳城赌博开户|
菲律宾太阳城赌博代理|
菲律宾太阳城博彩公司|
菲律宾太阳城AG|
Always around to get quality writing like this, I am very lucky to have found this, thank you !! If you are looking for an online toto site with the most junior agent in Indonesia and ready to give the biggest jackpot, you can try on the website https://kxndn.com/
Very high quality site, very neatly arranged and I found interesting things here, thank you. If you like playing games you can try interesting online games on the site https://haitikitchen.com/
Unexpectedly, I will get an interesting site like this, very high quality, thank you. If you are interested in a site that provides interesting and useful information, you can visit the website https://rodentsfacts.com/. Where the site is a site that specifically provides information about online slot games, to make it easier for players to get to know online slot games in more depth.
Very interesting site, here I got important points that are very useful, thank you. If you are interested in interesting information, we recommend the site https://tuvanxaydungnha.net/ , This is a site specifically designed to help online game players, which will make it easier to play and will know the game well. The information we provide is very accurate and the best, and every day updates.
Online slot games are very popular entertainment, this game is very easy to access with a variety of game choices with high-quality themes and music that can attract attention. This game has many profitable features such as attractive bonuses and promotions, this game has a fairly large chance of winning with very fast transactions. If you are interested in this game more completely, you can see it directly with just one click on my following link https://yntuytyon.com/
Very good writing, I really enjoyed it thank you. If you are interested in other best information you can see on the site https://bestservice141.com/. Which is a site that provides important and useful information about online slot games.
Very interesting site, thanks for the information. If you are interested in popular online games that provide a different sensation, you can try it at cupu toto . Which is the most popular online toto site.
A site that provides complete information about online slots to ease the difficulty of playing. https://phototypenbi.info/
Very interesting information thank you. If you are interested in other interesting information you can visit the website https://schokland.info/, which is the most popular online kasino information provider site.
Thank you very much for this very informative article! I really enjoyed my time here. Dont miss the site that provides online slots and lottery complete with interesting themes and music, and we have an excellent level of service. If you are interested, you can go directly to the website https://vladcole.com
What an interesting article, here I get an important point, thank you. If you are interested in a game that has an interesting playing sensation like online lottery, you can try it on the website https://peicardiganlobstersuppers.com/, One of the bookies that is very viral on Indonesian social media at the moment because it often provides big wins every day.
Отправить комментарий
Приветствуются комментарии:Для остальных вопросов и обсуждений есть форум.