function willNotChange(x) {
x = 1;}
var x = 1000;
willNotChange(x);
document.write('After function call, x = ' + x + '<br>'); // Still 1000
function willChange(y) {
y.num = 2;}
var y = {num: 2000};
willChange(y);document.write('After function call y.num = ' + y.num + '<br>'); // Now 2, not 2000
var myString = 'hello';
// Assign to a new variable (just like when you pass to a function)var param1 = myString;param1 = 'world'; // Re assignment
console.log(myString); // Logs 'hello'console.log(param1); // Logs 'world'
现在,同样的事情,但有一个功能
function myFunc(param1) {param1 = 'world';
console.log(param1); // Logs 'world'}
var myString = 'hello';// Calls myFunc and assigns param1 to myString just like param1 = myStringmyFunc(myString);
console.log(myString); // logs 'hello'
好的,现在让我们举几个使用对象的例子……首先,没有函数。
var myObject = {firstName: 'Joe',lastName: 'Smith'};
// Assign to a new variable (just like when you pass to a function)var otherObj = myObject;
// Let's mutate our objectotherObj.firstName = 'Sue'; // I guess Joe decided to be a girl
console.log(myObject.firstName); // Logs 'Sue'console.log(otherObj.firstName); // Logs 'Sue'
// Now, let's reassign the variableotherObj = {firstName: 'Jack',lastName: 'Frost'};
// Now, otherObj and myObject are assigned to 2 very different objects// And mutating one object has no influence on the otherconsole.log(myObject.firstName); // Logs 'Sue'console.log(otherObj.firstName); // Logs 'Jack';
现在,同样的事情,但有一个函数调用
function myFunc(otherObj) {
// Let's mutate our objectotherObj.firstName = 'Sue';console.log(otherObj.firstName); // Logs 'Sue'
// Now let's re-assignotherObj = {firstName: 'Jack',lastName: 'Frost'};console.log(otherObj.firstName); // Logs 'Jack'
// Again, otherObj and myObject are assigned to 2 very different objects// And mutating one object doesn't magically mutate the other}
var myObject = {firstName: 'Joe',lastName: 'Smith'};
// Calls myFunc and assigns otherObj to myObject just like otherObj = myObjectmyFunc(myObject);
console.log(myObject.firstName); // Logs 'Sue', just like before
function myFunc(myString) {// myString is private and does not affect the outer variablemyString = 'hello';}
var myString = 'test';myString = myString; // Does nothing, myString is still 'test';
myFunc(myString);console.log(myString); // Logs 'test'
/*The following C program demonstrates how argumentsto JavaScript functions are passed in a way analogousto pass-by-pointer-value in C. The original JavaScripttest case by @Shog9 follows with the translation ofthe code into C. This should make things clear tothose transitioning from C to JavaScript.
function changeStuff(num, obj1, obj2){num = num * 10;obj1.item = "changed";obj2 = {item: "changed"};}
var num = 10;var obj1 = {item: "unchanged"};var obj2 = {item: "unchanged"};changeStuff(num, obj1, obj2);console.log(num);console.log(obj1.item);console.log(obj2.item);
This produces the output:
10changedunchanged*/
#include <stdio.h>#include <stdlib.h>
struct obj {char *item;};
void changeStuff(int *num, struct obj *obj1, struct obj *obj2){// make pointer point to a new memory location// holding the new integer valueint *old_num = num;num = malloc(sizeof(int));*num = *old_num * 10;// make property of structure pointed to by pointer// point to the new valueobj1->item = "changed";// make pointer point to a new memory location// holding the new structure valueobj2 = malloc(sizeof(struct obj));obj2->item = "changed";free(num); // end of scopefree(obj2); // end of scope}
int num = 10;struct obj obj1 = { "unchanged" };struct obj obj2 = { "unchanged" };
int main(){// pass pointers by value: the pointers// will be copied into the argument list// of the called function and the copied// pointers will point to the same values// as the original pointerschangeStuff(&num, &obj1, &obj2);printf("%d\n", num);puts(obj1.item);puts(obj2.item);return 0;}
function changePrimitive(val) {// At this point there are two '10's in memory.// Changing one won't affect the otherval = val * 10;}var x = 10;changePrimitive(x);// x === 10
对一个对象重复此操作会产生不同的结果:
function changeObject(obj) {// At this point there are two references (x and obj) in memory,// but these both point to the same object.// changing the object will change the underlying object that// x and obj both hold a reference to.obj.val = obj.val * 10;}var x = { val: 10 };changeObject(x);// x === { val: 100 }
再举一个例子:
function changeObject(obj) {// Again there are two references (x and obj) in memory,// these both point to the same object.// now we create a completely new object and assign it.// obj's reference now points to the new object.// x's reference doesn't change.obj = { val: 100 };}var x = { val: 10 };changeObject(x);// x === { val: 10}
// codevar obj = {text: 'Hello world!'};
/* function parameters get their own pointer to* the arguments that are passed in, just like any other variable */someFunc(obj);
// illustration(caller scope) (someFunc scope)\ /\ /\ /\ /\ /{ }|||'Hello world'
var a = 2;var b = a; // `b` is always a copy of the value in `a`b++;a; // 2b; // 3
var c = [1,2,3];var d = c; // `d` is a reference to the shared `[1,2,3]` valued.push( 4 );c; // [1,2,3,4]d; // [1,2,3,4]
var a = {a: 1,b: 2,c: 3};var b = a;
// b.c is referencing to a.c valueconsole.log(b.c) // Output: 3// Changing value of b.cb.c = 4// Also changes the value of a.cconsole.log(a.c) // Output: 4
let arr = [1, 2, 3, 4, 5]; //arr is an object now and a purple arrow is indicating itlet obj2 = arr; // now, obj2 is another purple arrow that is indicating the value of arr objlet obj3 = ['a', 'b', 'c'];obj2.push(6); // first pic below - making a new hand for the blue circle to point the 6//obj2 = [1, 2, 3, 4, 5, 6]//arr = [1, 2, 3, 4, 5, 6]//we changed the blue circle object value (type1-value) and due to arr and obj2 are indicating that so both of them changedobj2 = obj3; //next pic below - changing the direction of obj2 array from blue circle to orange circle so obj2 is no more [1,2,3,4,5,6] and it's no more about changing anything in it but we completely changed its direction and now obj2 is pointing to obj3//obj2 = ['a', 'b', 'c'];//obj3 = ['a', 'b', 'c'];
let a = { prop: 1 };let b = a; // a and b hold the same valuea.prop = "test"; // The object gets mutated, can be observed through both a and bb = { prop: 2 }; // b holds now a different value