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

首頁 > 編程 > JavaScript > 正文

JavaScript: Passing by Value or by Reference

2019-11-11 03:52:07
字體:
來源:轉載
供稿:網友

原文: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
亚洲最新av网址| 性欧美长视频免费观看不卡| 日韩在线视频网站| 欧美一区二区视频97| 国产精品日韩专区| 最近2019中文字幕第三页视频| 国产精品福利网站| 久久中文字幕国产| 欧美高清videos高潮hd| 日韩国产欧美精品在线| 欧美成人在线免费| 国精产品一区一区三区有限在线| 国产成人一区二区三区电影| 国产午夜精品麻豆| 久久久国产成人精品| 成人在线视频福利| 国产欧美日韩免费看aⅴ视频| 国产91露脸中文字幕在线| 日韩精品极品在线观看播放免费视频| 欧美人与性动交a欧美精品| 亚洲欧洲国产伦综合| 久久精品人人做人人爽| 日韩少妇与小伙激情| 久久成人一区二区| 欧洲s码亚洲m码精品一区| 亚洲中国色老太| 中文字幕久久精品| 亚洲色图国产精品| 亚洲最大福利网站| 欧美一区二区影院| 九九热r在线视频精品| 一区二区欧美激情| 国产中文字幕91| 中文字幕综合一区| 欧美电影在线播放| 久久久国产精品亚洲一区| 亚洲国模精品一区| 欧美一级在线亚洲天堂| 亚洲欧美日韩高清| 在线精品视频视频中文字幕| 国产视频在线一区二区| www.99久久热国产日韩欧美.com| 亚洲精品国产精品久久清纯直播| 91香蕉嫩草影院入口| 日韩欧美国产免费播放| 欧美性xxxxhd| 国产精品三级美女白浆呻吟| 高清欧美一区二区三区| 日韩在线视频中文字幕| 在线免费观看羞羞视频一区二区| 日韩av在线免费播放| 国产精品一区二区久久久久| 啪一啪鲁一鲁2019在线视频| 中文字幕久热精品视频在线| 美女福利视频一区| 日韩欧美第一页| 欧美精品在线免费播放| 亚洲国产精品久久精品怡红院| 日本乱人伦a精品| 欧美在线视频在线播放完整版免费观看| 国产精品av免费在线观看| 亚洲视频国产视频| 国产精品人人做人人爽| 57pao成人永久免费视频| 懂色aⅴ精品一区二区三区蜜月| 日韩视频中文字幕| 亚洲精品电影网站| 成人a视频在线观看| 日韩精品欧美国产精品忘忧草| 国产精品一区二区三区在线播放| 国产成人久久久| 成人福利网站在线观看11| 欧美午夜视频在线观看| 亚洲国产另类 国产精品国产免费| 色av中文字幕一区| 国产在线98福利播放视频| 伊人青青综合网站| 欧美裸身视频免费观看| 欧美肥婆姓交大片| 亚洲第一精品久久忘忧草社区| 欧美午夜精品伦理| 精品香蕉在线观看视频一| 狠狠躁夜夜躁人人爽天天天天97| 久久综合88中文色鬼| 992tv成人免费视频| 久久久噜噜噜久噜久久| 久久精品久久久久久| 91精品国产高清久久久久久91| 亚洲激情中文字幕| 精品久久中文字幕久久av| 国产成人啪精品视频免费网| 亚洲a∨日韩av高清在线观看| 九色91av视频| 欧美日韩一区二区免费视频| 国产精品视频xxxx| 国产丝袜一区二区| 视频一区视频二区国产精品| 97香蕉久久超级碰碰高清版| 国产精品免费一区二区三区都可以| 欧美成人一二三| 亚洲国产精品免费| 午夜美女久久久久爽久久| 国产精品久久国产精品99gif| 久久精品国产一区二区三区| 欧美一区深夜视频| 国产美女精品视频免费观看| 国产亚洲精品一区二555| 久热99视频在线观看| 日本不卡免费高清视频| 亚洲欧美一区二区三区久久| 久久成人精品一区二区三区| 草民午夜欧美限制a级福利片| 55夜色66夜色国产精品视频| 国产日韩av在线播放| 欧美电影免费观看大全| 亚洲一区二区福利| 欧美激情手机在线视频| 欧美午夜丰满在线18影院| 亚洲xxxxx| 亚洲视频第一页| 97在线视频免费看| 亚洲午夜久久久久久久| 国产成人精品视频| 成人性生交xxxxx网站| 欧美日韩国产激情| 久久久久久亚洲精品| 精品中文字幕久久久久久| 亚洲激情免费观看| 亚洲精品电影网站| 欧美乱妇高清无乱码| 激情成人中文字幕| 亚洲综合在线做性| 久久人91精品久久久久久不卡| 中文字幕久久久av一区| 最近的2019中文字幕免费一页| 97国产真实伦对白精彩视频8| 色综合91久久精品中文字幕| 日韩高清av在线| 九九热精品视频在线播放| 国内精品久久久久久中文字幕| 日本久久久久亚洲中字幕| 久久91亚洲精品中文字幕| 国产综合久久久久久| 欧美成人在线网站| 国产免费成人av| 国产中文字幕日韩| 欧美激情亚洲另类| 在线视频欧美日韩精品| 亚洲在线www| 国产精品一区二区性色av| 久久久久久久成人| 亚洲乱码av中文一区二区| 国产精品老女人精品视频| 国产激情综合五月久久| 久久免费视频网站| 成人女保姆的销魂服务| 久久不射热爱视频精品| 国产日韩在线看片| 欧美一级在线播放| 欧美日韩午夜激情| 日韩av快播网址| 国产一区二区三区丝袜| 日韩精品极品视频| 日本久久久久久久久|