[問題] Java by value VS JS by reference
最近學了
JS的by refenerce用法
跟JAVA 的by value 去比較
我這邊的理解是一模一樣 為什麼名稱會不一樣呢?
這裡有點不太理解 希望有高人能指點一下
JS:
var c = { greeting: 'hi' };
var d;
d = c;
c.greeting = 'hello';
console.log(c);
console.log(d);
// by reference (even as parameters)
function changeGreeting(obj) {
obj.greeting = 'Hola'; // mutate
}
changeGreeting(d);
console.log(c);
console.log(d);
JAVA:
public static void main(String[] args) {
StringBuffer a = new StringBuffer("A");
StringBuffer b = new StringBuffer("B");
StringBuffer c = a;
operate(a,b);
System.out.println(a+","+b);//AB,B
System.out.println("c:" + c);
}
static void operate(StringBuffer x,StringBuffer y){
x.append(y);
y=x;
}
https://pjchender.blogspot.tw/2016/03/javascriptby-referenceby-value.html
這是那堂課的相關筆記 裡面提到JS是 by reference
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.192.30.30
※ 文章網址: https://www.ptt.cc/bbs/Ajax/M.1501304408.A.6F7.html
※ chiang0514:轉錄至看板 java 07/29 13:02
※ 編輯: chiang0514 (123.192.30.30), 07/29/2017 13:04:01
推
07/29 13:20, , 1F
07/29 13:20, 1F
→
07/29 14:37, , 2F
07/29 14:37, 2F
→
07/29 14:39, , 3F
07/29 14:39, 3F
※ 編輯: chiang0514 (123.192.30.30), 07/29/2017 14:41:40
推
07/29 15:03, , 4F
07/29 15:03, 4F
→
07/29 15:04, , 5F
07/29 15:04, 5F
→
07/29 15:32, , 6F
07/29 15:32, 6F
→
07/29 15:34, , 7F
07/29 15:34, 7F
→
07/29 15:37, , 8F
07/29 15:37, 8F
→
07/29 15:37, , 9F
07/29 15:37, 9F
→
07/29 15:49, , 10F
07/29 15:49, 10F
→
07/29 15:50, , 11F
07/29 15:50, 11F
推
07/29 16:02, , 12F
07/29 16:02, 12F
→
07/29 16:03, , 13F
07/29 16:03, 13F
→
07/29 16:04, , 14F
07/29 16:04, 14F
→
07/29 16:05, , 15F
07/29 16:05, 15F
推
07/29 16:16, , 16F
07/29 16:16, 16F
→
07/29 16:16, , 17F
07/29 16:16, 17F
→
07/30 09:39, , 18F
07/30 09:39, 18F
→
07/30 09:40, , 19F
07/30 09:40, 19F
→
07/30 09:42, , 20F
07/30 09:42, 20F
→
07/30 09:44, , 21F
07/30 09:44, 21F
Ajax 近期熱門文章
PTT數位生活區 即時熱門文章