/    Sign up×
Community /Pin to ProfileBookmark

Passing in objects to functions

Given the following code…

“`js
let person = {
name: “John”,
age: 32,
partTime: false
};

function incrementAge(person) {
person.age++;
}

incrementAge(person);

showMessage(person.age); //age is 33
“`

If `person` is an object, and `incrementAge` can modify its properties by reference, is `person` an instance of a class? If so, what class is that? I don’t recall creating a class and now that I think of it I don’t think JavaScript has classes…

If another function is introduced that increments the same property, would the new value be 34 or 33?

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@DescartesauthorSep 29.2020 — > @Descartes#1623765 If another function is introduced that increments the same property, would the new value be 34 or 33?

It would be 34. But I'm not sure what to make of this... here is the code...

``js<i>
</i>let person = {
name: "John",
age: 32,
partTime: false
};

function incrementAge(person) {
person.age++;
}

incrementAge(person);

showMessage(person.age); //age is 33

function anotherAger(person) {
person.age++;
}

anotherAger(person);

showMessage(person.age); //age is 34<i>
</i>
``
Copy linkTweet thisAlerts:
@DescartesauthorSep 29.2020 — I think what confuses me is the absence of a class. I have not instantiated a class, yet somehow I have an object. What I need to realize and accept here... I think... is that this is an object, not a class. So any changes made to this object will be persisted during its lifetime. (I'm coming from C# and in C# you have to instantiate a class to create a new object.)
Copy linkTweet thisAlerts:
@daveyerwinSep 30.2020 — Almost every thing in java script is a java script object.

https://www.w3schools.com/js/js_object_definition.asp

``<i>
</i>&lt;script&gt;

var a = [1,2,3];

a.sum=function(){
var ret = 0;
for(let i=0;i&lt;this.length;i++)
ret += this[i];
return ret;
}
a.push(4);
alert(a.sum())

&lt;/script&gt; <i>
</i>
``
Copy linkTweet thisAlerts:
@JMRKEROct 05.2020 — @Descartes#1623767 To see the results of either of your first two posts, you need to add:

function showMessage(info) { alert(info); }

to avoid console errors
Copy linkTweet thisAlerts:
@rpg2009Oct 08.2020 — > @Descartes#1623765 If person is an object, and incrementAge can modify its properties by reference, is person an instance of a class?

You are kind of right when you said Javascript doesn't have classes. So the answer is no.

The class implementation JS does have is considered syntactic sugar — under the hood its essentially prototypal inheritance.

What you defined is called an Object literal. By default it inherits directly from the base Object.prototype.

var obj = {}; Object.getPrototypeOf(obj) === Object.prototype // true

The getter/setter property that defines this link is called <STRONG>__proto__</STRONG> and albeit can be set is generally used as a read-only property. There are alternatives for setting the prototype.

Note console.dir is pretty useful if you want to examine what you have

<CODE>
`<i>
</i>console.dir(obj)
v Object
v __proto__:
&gt;constructor: ƒ Object()
&gt;hasOwnProperty: ƒ hasOwnProperty()
&gt;isPrototypeOf: ƒ isPrototypeOf()
&gt;propertyIsEnumerable: ƒ propertyIsEnumerable()
&gt;toLocaleString: ƒ toLocaleString()
&gt;toString: ƒ toString()
&gt;valueOf: ƒ valueOf()
&gt;__defineGetter__: ƒ __defineGetter__()
&gt;__defineSetter__: ƒ __defineSetter__()
&gt;__lookupGetter__: ƒ __lookupGetter__()
&gt;__lookupSetter__: ƒ __lookupSetter__()
&gt;get __proto__: ƒ __proto__()
&gt;set __proto__: ƒ __proto__()<i>
</i>
``
If interested I would suggest looking into JS functions (Specifically when invoked with 'new' as a constructor), JS prototypal inheritance and then JS Classes.
×

Success!

Help @Descartes spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 3.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,

tipper: Anonymous,
tipped: article
amount: 10 SATS,
)...