Skip to content

Javascript Pass Object Method As Argumentative Essay

JavaScript: Passing by Value or by Reference

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:

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:

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:

The problem here is the use of the ‘’ 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, 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 object and for functions called from an event, this would be the 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 from the last example you’d get the following:

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 method. is part of the JavaScript specification and allows a function to run in the context of another object. As a result, the keyword will reference the right object: the object we passed in.

Here’s our function one more time:

Happy Scripting!

[Javascript] Pass By Value And Pass By Reference In JavaScript

In this post, we will look into pass by value and pass by reference in Javascript.

Let’s see what is pass by value and pass by reference before looking into its javascript context.

Pass by Value:

In Pass by Value, Function is called by directly passing the value of the variable as the argument. Changing the argument inside the function doesn’t affect the variable passed from outside the function.

Javascript always pass by value so changing the value of the variable never changes the underlying primitive (String or number).

However, when a variable refers to an object which includes array, the value is the reference to the object.

Pass by Reference:

In Pass by Reference, Function is called by directly passing the reference/address of the variable as the argument. Changing the argument inside the function affect the variable passed from outside the function. In Javascript objects and arrays follows pass by reference.

so if we are passing object or array as an argument to the method, then there is a possibility that value of the object can change.

If you enjoyed this article, Please share it with your developer friends and in social media.

Follow our facebook page for latest updates.