javascript parameter passed by value

But remember the object itself is reference

Issue from my project TicTacToe

This project is a Tic-Tac-Toe game with unbeatable AI. The algorithm used is MinMax algorithm.

The code is after refactoring using require.js and underscore.js.


Explain the reason

The code :

Javascript parameter is passed by value, it means in the body of function, it will alway have a copy of the original parameter.

In the code, num and obj2 are good example of the concept. The changes are only limited in the function body.

But why is obj2 changed?

It’s because the object parameter itself is reference. [1, 2, 3] is array object, object2 = {name: “hello”} is an object, so in the function parameter list, they are references. Although in the body of the function, they will have a copy, the copy still points to the same object, so object.property_name or object[“property_name”] will change the original object’s property.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.