亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 編程 > JavaScript > 正文

JavaScript: Passing by Value or by Reference

2019-11-11 05:00:25
字體:
來源:轉載
供稿:網友

原文:https://snook.ca/archives/javascript/Javascript_pass

In JavaScript, we have functions and we have arguments that we pass into those functions. But how JavaScript handles what you’re passing in is not always clear. When you start getting into object-oriented development, you may find yourself perplexed over why you have access to values sometimes but not other times.

When passing in a PRimitive type variable like a string or a number, the value is passed in by value. This means that any changes to that variable while in the function are completely separate from anything that happens outside the function. Let’s take a look at the following example:

function myfunction(x){      // x is equal to 4      x = 5;      // x is now equal to 5}var x = 4;alert(x); // x is equal to 4myfunction(x); alert(x); // x is still equal to 4

Passing in an object, however, passes it in by reference.In this case, any property of that object is accessible within the function. Let’s take a look at another example:

function myobject(){	this.value = 5;}var o = new myobject();alert(o.value); // o.value = 5function objectchanger(fnc){	fnc.value = 6;}objectchanger(o);alert(o.value); // o.value is now equal to 6

So, what happens when you pass in a method of an object? Most would expect (or at least I did) that it would be passed by reference allowing the method to access other parts of the object it is apart of. Unfortunately, that’s not the case. Check out this example:

function myobject(){	this.value = 5;}myobject.prototype.add = function(){	this.value++;}var o = new myobject();alert(o.value); // o.value = 5o.add();alert(o.value); // o.value = 6function objectchanger(fnc){	fnc(); // runs the function being passed in}objectchanger(o.add);alert(o.value); // sorry, still just 6

The problem here is the use of the ‘this’ keyWord. It’s a handy short-hand for referring to the current object context. When passing a function as a parameter, though, the context is lost. More accurately, this now refers to the context of the object making the call instead of the object’s function we just passed in. For standalone functions, this would be the window object and for functions called from an event, this would be the event object.

Solving the problem

There are two possible ways to get around this.

Option 1: When you know the method

If you know the method of the object that will be called then it’s fairly easy. Just pass in the object instead of the function and call that instead. Using the objectchanger from the last example you’d get the following:

function objectchanger(obj){	obj.add(); // runs the method of the object being passed in}objectchanger(o);alert(o.value); // the value is now 7

Option 2: When you don’t know the method

If you don’t know the method of the object being passed in then you need to pass both the method and the object as parameters and use the call method. call is part of the JavaScript specification and allows a function to run in the context of another object. As a result, the this keyword will reference the right object: the object we passed in.

Here’s our objectchanger function one more time:

function objectchanger(fnc, obj){	fnc.call(obj); // runs the method of the object being passed in}objectchanger(o.add, o);alert(o.value); // the value is now 7

Happy Scripting!


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
91久久精品在线| 国模叶桐国产精品一区| 国产精品夜色7777狼人| 日韩国产高清视频在线| 成人欧美一区二区三区黑人| 亚洲欧美国产一区二区三区| 国产亚洲精品久久久久动| 国产中文欧美精品| 亚洲va欧美va国产综合剧情| 91日本视频在线| 在线看片第一页欧美| 欧美日韩成人黄色| 国产91精品久久久| 日韩欧美精品免费在线| 中文字幕亚洲综合| 欧美日韩国产丝袜另类| 日韩在线视频一区| 亚洲网站视频福利| 亚洲国产美女精品久久久久∴| 一区二区欧美在线| 欧美精品videosex牲欧美| 久久精品国产69国产精品亚洲| 91视频-88av| 国产精品网红直播| 国产有码在线一区二区视频| 久久亚洲影音av资源网| 尤物tv国产一区| 日本精品视频网站| 久久全球大尺度高清视频| 国产精品久久久亚洲| 亚洲国产精品美女| 一区二区三欧美| 7m精品福利视频导航| 精品久久久久久久久国产字幕| 91精品国产色综合| 成人h猎奇视频网站| 色婷婷亚洲mv天堂mv在影片| 午夜精品一区二区三区在线| 亚洲综合日韩中文字幕v在线| 国产精品免费一区豆花| 91理论片午午论夜理片久久| 精品国产91乱高清在线观看| 少妇高潮 亚洲精品| 国产精品一区二区久久久| 亚洲综合在线小说| 亚洲va欧美va国产综合剧情| 日韩在线不卡视频| 欧美成人午夜视频| 中文字幕亚洲欧美日韩在线不卡| www.日韩视频| 国产精品久久色| 91伊人影院在线播放| 国产在线精品播放| 成人在线视频福利| 久久精品国产久精国产一老狼| 国产精品入口福利| 欧美精品日韩三级| 国产精品成人观看视频国产奇米| 国产亚洲成精品久久| 亚洲永久在线观看| 欧美一级视频一区二区| 中文字幕v亚洲ⅴv天堂| 欧美成人免费va影院高清| 中文字幕日韩av综合精品| 欧美性20hd另类| 97视频免费在线观看| 欧美大片在线免费观看| 亚洲欧美国产精品| 91亚洲精品一区| 成人亚洲综合色就1024| 日韩经典一区二区三区| 日韩av在线免播放器| 中文字幕亚洲情99在线| 日本在线观看天堂男亚洲| 亚洲男人av在线| 4438全国成人免费| 色视频www在线播放国产成人| 久久国产一区二区三区| 欧洲日本亚洲国产区| 日韩成人在线观看| 伊人久久久久久久久久久久久| 亚洲天堂久久av| 国产亚洲人成网站在线观看| 欧美与黑人午夜性猛交久久久| 57pao精品| 97久久久久久| 韩国三级电影久久久久久| 亚洲电影免费观看高清完整版在线| 精品国模在线视频| 亚洲视频欧洲视频| 欧美色道久久88综合亚洲精品| xxx一区二区| 欧美日韩国产丝袜另类| 亚洲成人三级在线| 久久久精品在线观看| 中文字幕日韩av| 国产精品视频公开费视频| 国产精品成人久久久久| 国产精品一区久久| 精品久久久久久国产| 日本午夜精品理论片a级appf发布| 国产成人在线一区二区| 日韩欧美精品网址| 91九色综合久久| 国产精品久久久久久影视| 亚洲免费成人av电影| 欧美在线观看一区二区三区| 久久精品99无色码中文字幕| 91嫩草在线视频| 欧美成人午夜视频| 国产精品国产三级国产专播精品人| 成人黄色av网站| 国产成人精品视频在线| 黄网动漫久久久| 国产日韩中文在线| 国产一区二区三区日韩欧美| 夜夜嗨av一区二区三区免费区| 97久久精品在线| 日韩av网址在线| 国产日韩欧美中文在线播放| 久久理论片午夜琪琪电影网| 狠狠色狠色综合曰曰| 欧美最猛性xxxxx亚洲精品| 亚洲人成电影在线观看天堂色| www.日韩.com| 中文字幕日韩电影| 亚洲第一视频网| 在线免费观看羞羞视频一区二区| 韩国欧美亚洲国产| 成人黄色生活片| 国产小视频国产精品| 亚洲天天在线日亚洲洲精| 欧美午夜影院在线视频| 欧美极品第一页| 国产精品久久久久久久久| 日韩中文字幕在线精品| 日韩免费在线播放| 8x拔播拔播x8国产精品| 亚州国产精品久久久| 欧美在线视频免费播放| 亚洲乱码国产乱码精品精| 77777亚洲午夜久久多人| 欧美另类精品xxxx孕妇| 亚洲国产精品国自产拍av秋霞| 最近2019中文字幕mv免费看| 91精品国产乱码久久久久久蜜臀| 中文一区二区视频| 精品福利在线看| 精品亚洲va在线va天堂资源站| 26uuu另类亚洲欧美日本一| 精品av在线播放| 久久99精品久久久久久青青91| 国产免费久久av| 人人爽久久涩噜噜噜网站| 日本不卡高字幕在线2019| 欧美综合一区第一页| 欧美日韩国产页| 欧美猛交ⅹxxx乱大交视频| 亚洲精品美女在线观看| 国产不卡在线观看| 日本91av在线播放| 97热精品视频官网| 国产精品白嫩初高中害羞小美女| 奇米四色中文综合久久|