What I Learned from Teaching JavaScript, Part Two
In this article, MVB Sibeesh Venu walks us through using JavaScript continuations and constructors. If you're new to web dev or a seasoned pro, he offers up good advice.
Join the DZone community and get the full member experience.
Join For Freehere, we are going to a see an another article of the javascript series. in the first part , we saw some basics that you can start with. in this article, we will be discussing the javascript object continuations and constructors. you can always see my other posts related to javascript here . we will be using visual studio for our development. if you are totally new to javascript, i strongly recommend you to read some basics here . i hope you will like this. now let’s begin.
download the source code
do you know javascript – part two
background
this article is the second part of a series we have just started. if you haven’t read the first part yet, i strongly recommend you to read it here .
setting up the platform
to get started, we are going to create an html file and js file.
<!doctype html>
<html>
<head>
<title>do you know javascript? are you sure? part two</title>
<meta charset="utf-8" />
<script src="javascript.js"></script>
</head>
<body>
</body>
</html>
let’s begin our tutorial
now, please open your javascript file. we have some scripts to write.
constructor
have you ever created a constructor in javascript? before creating one, it is better to know what it is the needed to do. let’s start with an example. here, i am going to create an object called 'bike.'
var bike = {
name: "thnuderbird",
color: "blue",
cc: "350",
company: "royal enfield",
tankcapacity: "20",
abs: false,
showname: function() {
console.log(this.name);
}
};
as you can read it, this object is holding the common properties of my bike royal enfield thunderbird. now, my question is: is this is the only bike you know? absolutely not, right? so suppose you need to create an object for the brand new bajaj dominor? what would you do? create another object? so what if you know more than 10 bikes. if your answer is creating 10 objects, that’s not fair at all. instead, why don’t we share the common properties like name, color, cc, etc. so we are going to create objects, but we are not going to create the properties again. so let’s create an object for the dominor. before doing that, we need to create a bike constructor as follows.
function bike(name, color, cc, company, tankcapacity, abs) {
this.name = name;
this.color = color;
this.cc = cc;
this.company = company;
this.tankcapacity = tankcapacity;
this.abs = abs;
this.showname = function () {
console.log('the name of the currnet bike is ' + this.name);
}
}
now we can create an object for the dominor as follows. remember, when you write the word bike, you can see that the intellisense is shown for you as follows.
constructor_intellisence
var dominor = new bike("dominor", "black", "bajaj", "20", true);
dominor.showname();
notice that we have not created the function
showname
for the object dominor, but in bike, and we are still able to use that in the object dominor. and you can see an output as follows in your browser console:
dominor_object_output
and you can create an object for royal enfield thunderbird as follows:
var thunderbird = new bike("thunderbird", "marine", "royal enfield", "20", false);
thunderbird.showname();
thunderbird_output
distinguish between own and inherited properties
as the heading implies, we have two kinds of properties: own and inherited. let’s create an example to understand that.
var thunderbird = new bike("thunderbird", "marine", "royal enfield", "20", false);
thunderbird.ismine = true;
console.log(dominor.name + " is yours or not? ");
console.log("ismine" in dominor);
console.log(thunderbird.name + " is yours or not? ");
console.log("ismine" in thunderbird);
console.log("tostring" in dominor);
console.log("tostring" in thunderbird);
before running it, guess what the output will be for the above code block. if you find the answer, check whether your answer matches the below output:
own_and_inherited_properties
so
ismine
is the property that we just added to the object
thunderbird
, and the same is not available in the object
dominor
. that’s cool. but why is the property
tostring
available in both objects, have we not added that to our object correctly? this is because the
tostring
method is being inherited from the object.prototype.
use of hasownproperty
in the above code, we have just seen how to check if any property is available in our object, but that doesn’t mean it is its own property, right? to check that, we can use the
hasownproperty
. let’s find out how to use it now.
console.log(dominor.name + " is yours or not? ");
console.log(dominor.hasownproperty("ismine"));
console.log(thunderbird.name + " is yours or not? ");
console.log(thunderbird.hasownproperty("ismine"));
console.log(dominor.hasownproperty("tostring"));
console.log(thunderbird.hasownproperty("tostring"));
once again, please try to answer it your own, before you run it.
hasownproperty_output
looping through the object
to loop through each item in an object, you can use the loop as follows:
for (var itm in thunderbird) {
console.log(itm);
}
this is not the preferred way as we have not checked for
hasownproperties
, we are supposed to iterate only the properties which are its own.
looping_through_the_items_in_an_object
so let’s rewrite the above code as follows:
for (var itm in thunderbird) {
if (thunderbird.hasownproperty(itm)) {
console.log(itm + ":" + thunderbird[itm]);
}
}
here is the output:
iteration_with_hasownproperty
have we seen enough about objects? any idea how you can delete the property from an object?
delete thunderbird.ismine;
for (var itm in thunderbird) {
if (thunderbird.hasownproperty(itm)) {
console.log(itm + ":" + thunderbird[itm]);
}
}
delete_from_an_object
now it is time for a question. what would be the output of the following code?
console.log(delete thunderbird.tostring);
it will return true, now run it again. what is the output? again true? this is because
tostring
is an inherited property, so it won’t get deleted. so
thunderbird.tostring
will always give you this output.
that’s all for today. you can always download the source code attached to see the complete code and application. happy coding!.
references
see also
Published at DZone with permission of Sibeesh Venu, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments