Does JavaScript pass by reference or pass by value? I’m confused about the my parameter in the rectangle function. It starts as undefined and is redefined inside the function. If I remove it from the parameters, the inner area function can’t access it.
Is this a closure? But no function is returned. Here’s an example:
var shape = function (config) {
var that = {};
that.name = config.name || “”;
that.area = function () {
return 0;
};
return that;
};
var rectangle = function (config, my) {
my = my || {};
my.l = config.length || 1;
my.w = config.width || 1;
var that = shape(config);
that.area = function () {
return my.l * my.w;
};
return that;
};
myShape = shape({
name: “Unknown”
});
myRec = rectangle({
name: “Rectangle”,
length: 4,
width: 6
});
console.log(myShape.name + " area is " + myShape.area() + " " + myRec.name + " area is " + myRec.area());
Can you clarify this for me?
Hello!
In JavaScript, values are passed by value, but it’s essential to understand the difference between primitive types and objects.
-
Primitive types (numbers, strings, booleans, etc.) are passed by value, which means when you pass a primitive to a function, you’re passing a copy of the actual value. Any changes made inside the function won’t affect the original variable.
-
Objects (including arrays and functions), on the other hand, are passed by reference, but there’s a catch! While you pass a reference to the object, that reference is passed by value. This means you can modify the object’s properties, but you cannot change the reference itself. For example, reassigning the reference inside a function won’t affect the original object outside of the function.
This subtle distinction can be a bit tricky but is key to understanding how data is handled in JavaScript.
Let me know if you have more questions!
Thank you
Hello everyone!
Regarding your question about the my
parameter in the rectangle function, it’s important to note that my
begins as undefined
if no argument is passed. Inside the function, we redefine it with my = my || {}
, which ensures it becomes an empty object if it remains undefined. This approach allows us to safely add properties like l
(length) and w
(width) to the my
object without encountering any errors. If you were to remove my
from the parameters, the inner area
function wouldn’t be able to access it, as it wouldn’t be defined in the local scope. This design choice enhances the flexibility and robustness of the function.
Hello everyone!
The area function exemplifies a closure because it captures the lexical scope of the rectangle function. This means that it retains access to the variable my
even when area
is called outside the context of the rectangle function. Closures are powerful because they enable inner functions to remember their creation environment, preserving access to variables that would otherwise be out of scope. This feature is essential for creating functions with private state and for maintaining data integrity in more complex applications.