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

首頁 > 編程 > JavaScript > 正文

JavaScript: Passing by Value or by Reference

2019-11-11 03:25: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
精品国产拍在线观看| 亚洲男人的天堂网站| 欧美成人精品三级在线观看| 92版电视剧仙鹤神针在线观看| 国产精品久久久久久久久男| 成年无码av片在线| 日韩av中文字幕在线免费观看| 久久免费国产视频| 国产精品入口夜色视频大尺度| 91在线视频免费| 色先锋资源久久综合5566| 国产欧美在线看| 国模gogo一区二区大胆私拍| 欧美日韩国产页| 97香蕉久久夜色精品国产| 国模吧一区二区| 久青草国产97香蕉在线视频| 日韩精品视频在线| 国产欧美韩国高清| 亚洲欧美日韩天堂一区二区| 国产精品午夜国产小视频| 久久九九有精品国产23| 国产一区二区三区久久精品| 亚洲人成网站色ww在线| 国产999精品| 中文字幕亚洲精品| 日本午夜精品理论片a级appf发布| 日韩精品免费在线观看| 久久久爽爽爽美女图片| 97在线精品国自产拍中文| 日韩免费av在线| 欧美又大粗又爽又黄大片视频| 国产在线拍揄自揄视频不卡99| 最近2019中文免费高清视频观看www99| 欧美日韩中文在线观看| 亚洲美女www午夜| 成年无码av片在线| 久久成人这里只有精品| 夜夜嗨av一区二区三区免费区| 国产精品免费电影| 日韩美女视频免费在线观看| 中文字幕日韩精品在线观看| 91精品国产自产在线| 欧美在线中文字幕| 欧美成人四级hd版| 欧美乱大交xxxxx另类电影| 不卡中文字幕av| 国产不卡精品视男人的天堂| 欧美日韩成人网| 亚洲综合在线做性| 欧美亚洲视频在线观看| 91性高湖久久久久久久久_久久99| 亚洲第一二三四五区| 亚洲a级在线播放观看| 欧美亚洲午夜视频在线观看| 欧美伊久线香蕉线新在线| 日韩国产中文字幕| 日韩免费在线看| 欧美黑人性生活视频| www欧美日韩| 日韩精品中文字幕在线播放| 亚洲人成电影网站色| 91久热免费在线视频| 欧美精品www在线观看| 国产日韩av在线| 伊人久久久久久久久久| 88xx成人精品| 91久久久久久久久久久| 国产精品美女www爽爽爽视频| 久久精品亚洲94久久精品| 久久视频在线看| 午夜精品久久17c| 欧美激情一区二区久久久| 亚洲天堂免费观看| 亚洲综合最新在线| 日韩av网站电影| 久久精品亚洲国产| 欧美噜噜久久久xxx| 色婷婷久久av| 欧美国产日韩xxxxx| 欧美午夜视频在线观看| 成人日韩av在线| 日韩国产中文字幕| 久久久精品久久久久| 欧美日韩综合视频网址| 欧美黄网免费在线观看| 亚洲三级av在线| 一级做a爰片久久毛片美女图片| 久久久久这里只有精品| 亚洲人成在线一二| 日韩av免费一区| 夜色77av精品影院| 成人午夜小视频| 丁香五六月婷婷久久激情| 久久国产天堂福利天堂| 欧美日韩成人精品| 影音先锋欧美精品| 成人国产精品久久久| 国产激情久久久久| 亚洲国产精品va在线看黑人| 欧美日韩亚洲系列| 国产精品海角社区在线观看| 日本国产一区二区三区| 九九综合九九综合| 国产精品久久久久秋霞鲁丝| 亚洲一区二区久久久久久久| 日韩av免费看网站| 91久久久久久久一区二区| 日韩av网站电影| 亚洲第一福利视频| 亚州成人av在线| 91成人精品网站| 亚洲国产精品va在线看黑人| 日本电影亚洲天堂| 国产在线观看一区二区三区| 黄色精品一区二区| 久久久日本电影| 国产91成人在在线播放| 91精品国产色综合久久不卡98口| 午夜精品三级视频福利| 国产午夜精品一区理论片飘花| 日韩美女免费线视频| 久久精品视频导航| 亚洲91av视频| 日本中文字幕不卡免费| 久久视频在线免费观看| 午夜精品久久久久久99热| 91精品91久久久久久| 亚洲精品短视频| 久久久av电影| 欧美午夜精品久久久久久浪潮| 国产精品成人观看视频国产奇米| 欧美精品在线第一页| 不卡av在线网站| 81精品国产乱码久久久久久| 91九色精品视频| 亚洲国产精品热久久| 欧美多人乱p欧美4p久久| 国产精品羞羞答答| 国产综合在线看| 欧美色图在线视频| 欧美国产日韩精品| 97久久精品人人澡人人爽缅北| 欧美视频在线免费看| 亚洲全黄一级网站| 欧美高清视频在线观看| 国产亚洲精品日韩| 日韩大陆欧美高清视频区| 欧美亚洲国产精品| 日韩av手机在线| 上原亚衣av一区二区三区| 亚洲国产一区二区三区四区| 激情久久av一区av二区av三区| 8050国产精品久久久久久| 中文字幕亚洲一区二区三区五十路| 4438全国亚洲精品在线观看视频| 国产视频精品自拍| 亚洲成人黄色网| 色妞色视频一区二区三区四区| 在线a欧美视频| 欧美日本黄视频| 这里只有精品视频在线| 91免费视频网站| 91亚洲va在线va天堂va国|