DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Languages
  4. What I Learned from Teaching JavaScript, Part Two

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.

Sibeesh Venu user avatar by
Sibeesh Venu
·
Mar. 10, 17 · Tutorial
Like (14)
Save
Tweet
Share
7.71K Views

Join the DZone community and get the full member experience.

Join For Free

here, 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.

image title


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:

image title

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();


image title

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:

image title

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.

image title

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.

image title

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:

image title

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]);
    }
}


image title

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

  • js
  • see also

  • articles related to javascript
  • JavaScript Object (computer science) Property (programming)

    Published at DZone with permission of Sibeesh Venu, DZone MVB. See the original article here.

    Opinions expressed by DZone contributors are their own.

    Popular on DZone

    • Understanding and Solving the AWS Lambda Cold Start Problem
    • Choosing the Right Framework for Your Project
    • Fixing Bottlenecks in Your Microservices App Flows
    • Best CI/CD Tools for DevOps: A Review of the Top 10

    Comments

    Partner Resources

    X

    ABOUT US

    • About DZone
    • Send feedback
    • Careers
    • Sitemap

    ADVERTISE

    • Advertise with DZone

    CONTRIBUTE ON DZONE

    • Article Submission Guidelines
    • Become a Contributor
    • Visit the Writers' Zone

    LEGAL

    • Terms of Service
    • Privacy Policy

    CONTACT US

    • 600 Park Offices Drive
    • Suite 300
    • Durham, NC 27709
    • support@dzone.com
    • +1 (919) 678-0300

    Let's be friends: