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

首頁 > 編程 > JavaScript > 正文

JavaScript: Passing by Value or by Reference

2019-11-11 03:24:45
字體:
來源:轉載
供稿:網友

原文: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
亚洲人成网站免费播放| 欧美日韩亚洲一区二| 日韩精品中文字幕在线播放| 亚洲乱码国产乱码精品精天堂| 欧美成人免费一级人片100| 欧美极品少妇xxxxⅹ裸体艺术| 亚洲精品电影网站| 久久不射电影网| 亚洲а∨天堂久久精品喷水| 国产精品免费视频xxxx| 日韩精品一二三四区| 日韩精品在线电影| 国产mv久久久| 欧美精品一区在线播放| 国产精品99免视看9| 国产福利成人在线| 亚洲自拍小视频| 成年人精品视频| 欧美日韩激情网| 精品欧美激情精品一区| 91色琪琪电影亚洲精品久久| 国产成人亚洲综合青青| 欧美大肥婆大肥bbbbb| 亚洲欧美日韩区| 久久精品国产综合| 欧美大尺度电影在线观看| 精品成人国产在线观看男人呻吟| 亚洲a在线观看| 中文字幕亚洲欧美日韩高清| 最近2019年中文视频免费在线观看| 日韩中文字幕视频在线观看| 中文字幕国产亚洲| 久久99热这里只有精品国产| 久久精品影视伊人网| 久久久久亚洲精品| 日韩一区二区三区国产| 亚洲电影免费在线观看| 69视频在线播放| 亚洲欧洲日产国码av系列天堂| 日韩国产精品一区| 日韩欧美成人区| 中文字幕最新精品| 国产精品久久久久久搜索| 国精产品一区一区三区有限在线| 亚洲精品成人久久| 午夜精品久久久99热福利| 久久久噜噜噜久久| 一级做a爰片久久毛片美女图片| 久久免费视频在线观看| 亚洲福利视频免费观看| 日韩av毛片网| 久久在线免费视频| 日韩电影视频免费| 欧美午夜丰满在线18影院| 久久久视频精品| 欧美日韩爱爱视频| 精品久久久久久久久久久久久久| 精品夜色国产国偷在线| 色噜噜国产精品视频一区二区| 国产精品三级美女白浆呻吟| 欧美成人精品在线| 亚洲国产女人aaa毛片在线| 日本人成精品视频在线| 中文字幕亚洲欧美一区二区三区| 国产精品久久网| 日韩国产精品亚洲а∨天堂免| 亚洲一区二区三区视频| 亚洲福利视频网站| 日韩电视剧免费观看网站| 91成人国产在线观看| 91免费人成网站在线观看18| 国产成人av网址| 日韩av片免费在线观看| 国产区亚洲区欧美区| 亚洲激情视频网| 性欧美暴力猛交69hd| 亚洲一区二区久久久久久久| 97精品国产97久久久久久春色| 成人免费视频xnxx.com| 日韩精品小视频| 亚洲国产99精品国自产| 中文字幕欧美在线| 奇米一区二区三区四区久久| 一区国产精品视频| 国语自产精品视频在免费| 亚洲国产精久久久久久久| 亚洲人成电影在线观看天堂色| 懂色av一区二区三区| 91欧美精品午夜性色福利在线| 欧美国产日韩一区| 色偷偷888欧美精品久久久| 国产极品精品在线观看| 亚洲精品大尺度| 疯狂做受xxxx欧美肥白少妇| 欧美成人免费视频| 亚洲成人xxx| 亚洲午夜未满十八勿入免费观看全集| 欧美极品在线播放| 国产精品无码专区在线观看| 亚洲国产另类 国产精品国产免费| 91精品国产乱码久久久久久蜜臀| 久久久久中文字幕2018| 日韩久久免费视频| 国产精品第一区| 久久精品99久久久久久久久| 69久久夜色精品国产69乱青草| 精品视频在线播放色网色视频| 国产精品久久久久免费a∨| 在线亚洲午夜片av大片| 国产精品美女免费| 国产专区精品视频| 精品国产一区二区三区久久| 性视频1819p久久| 中文字幕日韩欧美在线| 在线观看国产成人av片| 免费不卡欧美自拍视频| 色综合久久88色综合天天看泰| 正在播放亚洲1区| 国产成人综合精品在线| 国产精品r级在线| 91久久国产精品| 精品久久久久久久久久| 国产在线精品一区免费香蕉| 国产成人精品电影久久久| 这里只有精品视频在线| 国产欧美日韩中文字幕在线| 国内精品模特av私拍在线观看| 日韩有码视频在线| 爽爽爽爽爽爽爽成人免费观看| 91日韩在线播放| 亚洲国产欧美一区| 亚洲第一视频网站| 日韩美女中文字幕| 亚洲一级黄色片| 久久综合国产精品台湾中文娱乐网| 青青草国产精品一区二区| 91干在线观看| 国产精品美女免费视频| 日韩美女中文字幕| 亚洲成人教育av| 亚洲激情 国产| 欧美综合在线第二页| 精品亚洲国产视频| 91性高湖久久久久久久久_久久99| 亚洲免费视频网站| 亚洲a一级视频| 精品中文字幕在线2019| 久久久久久久电影一区| 91久久久久久久久久久久久| 国产91色在线|免| 欧美一级成年大片在线观看| 亚洲精品电影网在线观看| 精品国产一区二区三区久久久| 91沈先生在线观看| 久久视频这里只有精品| 久久手机精品视频| 91亚洲精品久久久久久久久久久久| 日韩精品欧美国产精品忘忧草| 亚洲91精品在线| 欧美麻豆久久久久久中文| 日韩欧美国产免费播放| 亚洲无亚洲人成网站77777| 91在线观看免费高清完整版在线观看| 欧美电影在线观看完整版|