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

首頁 > 編程 > JavaScript > 正文

JavaScript: Passing by Value or by Reference

2019-11-11 02:55:15
字體:
來源:轉載
供稿:網友

原文: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亚洲一区精品| 亚洲电影免费观看高清完整版| 亚洲国产中文字幕久久网| 综合网中文字幕| 国产精品一区专区欧美日韩| 青草成人免费视频| 日韩免费av在线| 上原亚衣av一区二区三区| 亚洲国产精品久久久久秋霞蜜臀| 亚洲免费一级电影| 亚洲视频一区二区三区| 久久久爽爽爽美女图片| 日韩美女中文字幕| 久久精品中文字幕| 97精品在线视频| 69视频在线播放| 亚洲一区二区久久久久久| 色狠狠av一区二区三区香蕉蜜桃| 欧美高清一级大片| 国产精品免费看久久久香蕉| 色妞一区二区三区| 国产精品久久久久久久久男| 亚洲人午夜色婷婷| 中文字幕自拍vr一区二区三区| 亚洲bt欧美bt日本bt| 亚洲一区二区中文字幕| 国产日韩精品视频| 色青青草原桃花久久综合| 国产成人97精品免费看片| 亚洲一级黄色片| 欧美裸体xxxx极品少妇| 日本一区二三区好的精华液| 久久青草精品视频免费观看| 岛国av一区二区在线在线观看| 国产精品亚洲视频在线观看| 欧美成人久久久| 欧美精品电影免费在线观看| 国产精品久久久久7777婷婷| 久久久欧美一区二区| 中文字幕亚洲欧美一区二区三区| 国产亚洲激情在线| 亚洲精品成人久久电影| 亚洲精品综合久久中文字幕| 欧美亚洲激情在线| 欧美性少妇18aaaa视频| 国产最新精品视频| 成人激情视频在线播放| 91精品在线播放| 国产午夜精品视频免费不卡69堂| 久久综合五月天| 中文日韩在线视频| 色综合久久88色综合天天看泰| 亚洲欧美日本另类| 久久躁日日躁aaaaxxxx| 国产精品99久久久久久白浆小说| 欧美大学生性色视频| 国产精品久久久久久久久久99| 国产精品国产福利国产秒拍| 国产美女精品视频| 国产欧美最新羞羞视频在线观看| 一本色道久久88亚洲综合88| 成人免费大片黄在线播放| 亚洲第一av网站| 亚洲人成在线免费观看| 日韩欧美主播在线| 欧美性xxxx极品hd欧美风情| 亚洲精品电影网站| 国产精品热视频| 中文字幕亚洲欧美| 国产69精品久久久久9| 国产精品视频网| 中文字幕精品影院| 欧美日韩精品国产| 亚洲性xxxx| 国产精品入口免费视| 精品国产91久久久久久老师| 国产精品日日做人人爱| 91精品免费视频| 久久99热这里只有精品国产| 国产日韩欧美综合| 亚洲国产女人aaa毛片在线| 国产亚洲欧洲高清一区| 成人激情免费在线| 欧美国产欧美亚洲国产日韩mv天天看完整| 91超碰caoporn97人人| 国产亚洲人成网站在线观看| 国产精品www色诱视频| 米奇精品一区二区三区在线观看| 欧美久久精品一级黑人c片| 国产精品免费看久久久香蕉| 色噜噜狠狠狠综合曰曰曰| 成人av色在线观看| 国产成人亚洲综合91精品| 久久99久久亚洲国产| 成人免费网站在线看| 国内精品久久久| 国产精品福利久久久| 91久久国产婷婷一区二区| 日韩精品免费在线视频| 欧美一区二区三区四区在线| 亚洲精品综合久久中文字幕| 国产精品久久久久久av福利| 亚洲国产精品小视频| 欧美精品九九久久| 欧美精品情趣视频| 久久婷婷国产麻豆91天堂| 欧美亚洲一级片| 91久久国产综合久久91精品网站| 国产精品视频1区| 狠狠色香婷婷久久亚洲精品| 亚洲成人精品视频在线观看| 欧美激情喷水视频| 日韩视频―中文字幕| 欧美性受xxxx黑人猛交| 国产欧美精品一区二区三区介绍| 亚洲国产精品久久91精品| 国产成人+综合亚洲+天堂| 26uuu日韩精品一区二区| 久久久久亚洲精品国产| 国产成人精品久久亚洲高清不卡| 96pao国产成视频永久免费| 亚洲成人精品视频在线观看| 亚洲黄页视频免费观看| 国模精品视频一区二区三区| 欧洲一区二区视频| 精品亚洲一区二区三区| 亚洲人成网站999久久久综合| 中文字幕无线精品亚洲乱码一区| 在线精品国产欧美| 岛国av一区二区| 亚洲缚视频在线观看| 国产日韩中文字幕在线| 欧美成人激情在线| 久久久av免费| 欧美精品久久久久| 欧美成人精品h版在线观看| 亚洲欧美综合另类中字| 欧美成年人在线观看| 国产欧美一区二区三区在线看| 久久久久久久久久久91| 国产精品白嫩美女在线观看| 国产自摸综合网| 国产精品久久久久久久天堂| 亚洲精品美女在线观看播放| 国产精品男人的天堂| 色综合久久悠悠| 国产精品国产三级国产aⅴ浪潮| 欧美激情欧美激情在线五月| 国产日韩精品一区二区| 国产裸体写真av一区二区| 成人深夜直播免费观看| 久久艳片www.17c.com| 久久福利视频导航| 国产亚洲精品激情久久| 一区二区欧美亚洲| 欧美在线影院在线视频| 欧美高清性猛交| 中文字幕亚洲在线| 日韩国产精品亚洲а∨天堂免| 久久不射电影网| 久久乐国产精品| 久热精品在线视频| 亚洲欧美国产精品久久久久久久|