Sibeesh Passion

Top Menu

  • Home
  • Search
  • About
  • Privacy Policy

Main Menu

  • Articles
    • Azure
    • .NET
    • IoT
    • JavaScript
    • Career Advice
    • Interview
    • Angular
    • Node JS
    • JQuery
    • Knockout JS
    • Jasmine Framework
    • SQL
    • MongoDB
    • MySQL
    • WordPress
  • Contributions
    • Medium
    • GitHub
    • Stack Overflow
    • Unsplash
    • ASP.NET Forum
    • C# Corner
    • Code Project
    • DZone
    • MSDN
  • Social Media
    • LinkedIn
    • Facebook
    • Instagram
    • Twitter
  • YouTube
    • Sibeesh Venu
    • Sibeesh Passion
  • Awards
  • Home
  • Search
  • About
  • Privacy Policy

logo

Sibeesh Passion

  • Articles
    • Azure
    • .NET
    • IoT
    • JavaScript
    • Career Advice
    • Interview
    • Angular
    • Node JS
    • JQuery
    • Knockout JS
    • Jasmine Framework
    • SQL
    • MongoDB
    • MySQL
    • WordPress
  • Contributions
    • Medium
    • GitHub
    • Stack Overflow
    • Unsplash
    • ASP.NET Forum
    • C# Corner
    • Code Project
    • DZone
    • MSDN
  • Social Media
    • LinkedIn
    • Facebook
    • Instagram
    • Twitter
  • YouTube
    • Sibeesh Venu
    • Sibeesh Passion
  • Awards
  • Linux Azure Function Isolated Dot Net 9 YAML Template Deployment

  • Build, Deploy, Configure CI &CD Your Static Website in 5 mins

  • Post Messages to Microsoft Teams Using Python

  • Get Azure Blob Storage Blob Metadata Using PowerShell

  • Deploy .net 6 App to Azure from Azure DevOps using Pipelines

How toJavaScript
Home›How to›Do you know JavaScript? Are you sure? – Part Two

Do you know JavaScript? Are you sure? – Part Two

By SibeeshVenu
March 5, 2017
2491
0
Share:
Thunderbird_output

Here we are going to a see an anothe article of the JavaScript series. In the first part, we have see some basics that you can start with. In this article, we will be discussing about the JavaScript object continuations and constructors etc. 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 source code

  • Do you know JavaScript – Part Two
  • Background

    This article is the second part of the series we have just started. If you haven’t read the first part yet, I stronglyh recommed you to read it here.

    Setting up the platform

    To get started with, here we are going to create an HTML file and JS file.

    [html]
    <!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>
    [/html]

    Let’s begin our tutorial

    Now, please open your JavaScript file. We have some scripts to write.

    Constructor

    Have you ever created any constructor in JavaScript? Before going to create a one, it is beter to know what is the need for it. Let’s start with an example. Here I am going to create an object called bike.

    [js]
    var bike = {
    name: "Thnuderbird",
    color: "Blue",
    CC: "350",
    company: "Royal Enfield",
    tankCapacity: "20",
    abs: false,
    showName: function () {
    console.log(this.name);
    }
    };
    [/js]

    As you can read it, this object if holdong the common properties of my bike Royal Enfield Thunderbird. Now my question is this is the only bike you know? Absolutely no, right? So suppose you need to create an object for the brand new Bajaj Dominor? What will you do? Creating an another object? So what if you know more than 10 bikes. If your anser is creating 10 objects, that’s not fair at all. Instead, why don’t we share this common properties like name, color, cc ect…So we are going to create objects but we are not going to create the properties again. So let’s crete an object for Dominor. Before doing that, we need to create a bike construcor as follows.

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

    Now we can create object for Dominor as follows. Remember, when you write the word Bike, you can see that the intellisense is shown for you as follows.

    Constructor_intellisence

    Constructor_intellisence

    [js]
    var dominor = new Bike("Dominor", "Black", "Bajaj", "20", true);
    dominor.showName();
    [/js]

    Have you noticed that we have not created the function showName in 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 preceding in your browser console.

    Dominor_object_output

    Dominor_object_output

    And you can create an object for Royal Enfield Thunderbird as follows.

    [js]
    var thunderbird = new Bike("Thunderbird", "Marine", "Royal Enfield", "20", false);
    thunderbird.showName();
    [/js]

    Thunderbird_output

    Thunderbird_output

    Distinguish between own and inherited properties

    As the heading implies, we have two kind of properties, own and inherited. Let’s create an example to understand that.

    [js]
    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);
    [/js]

    Before runningit, Can you please guess what will be the output of the above coce block? If you find the answer, check whether your answer matches the below output.

    Own_and_inherited_properties

    Own_and_inherited_properties

    So isMine is the property that we jsut added to the object thunderbird, and the same is not available in the object dominor. That’s cool. But why the property toString is available in both object, we have not added that to our object right? This is because toString method is being inherited from the Object.prototype.

    Use of hasOwnProperty

    In the above code, we have just seen how to check 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.

    [js]
    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"));
    [/js]

    Once again, please try to answer it your own, before you run it.

    hasOwnProperty_output

    hasOwnProperty_output

    Looping through the object

    To loopthrough the each items in an object, you can use for loop as follow.

    [js]
    for (var itm in thunderbird) {
    console.log(itm);
    }
    [/js]

    This is not the preffered way as we have not checked for hasOwnProperties, we are suppose to iterate only the properties which is its own.

    Looping_through_the_items_in_an_Object

    Looping_through_the_items_in_an_Object

    So let’s rewrite the above code as follows.

    [js]
    for (var itm in thunderbird) {
    if (thunderbird.hasOwnProperty(itm)) {
    console.log(itm + ":" + thunderbird[itm]);
    }
    }
    [/js]

    Here is the output.

    Iteration_with_hasOwnProperty

    Iteration_with_hasOwnProperty

    We have seen enough about objects right? Any idea how you can delete the property from an object?

    [js]
    delete thunderbird.isMine;
    for (var itm in thunderbird) {
    if (thunderbird.hasOwnProperty(itm)) {
    console.log(itm + ":" + thunderbird[itm]);
    }
    }
    [/js]

    Delete_from_an_object

    Delete_from_an_object

    Now it is time for a question. What would be the output of the preceding code?

    [js]
    console.log(delete thunderbird.toString);
    [/js]

    It will return true, now run it again. What is the output? Again true? This is because of, toString is an inherited property so it won’t get deleted. So thunderbird.toString will always give you 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
  • Conclusion

    Did I miss anything that you may think which is needed? Could you find this post as useful? I hope you liked this article. Please share me your valuable suggestions and feedback.

    Your turn. What do you think?

    A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, Asp.Net Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.

    Kindest Regards
    Sibeesh Venu

    TagsJavascriptJavaScript BasicsJavaScript In Simple LanguageJavaScript ObjectsJavaScript Tutorial
    Previous Article

    Knockout JS Validations, Without a Plugin and ...

    Next Article

    Why Visual Studio 2017? Let us try ...

    0
    Shares
    • 0
    • +
    • 0
    • 0
    • 0

    SibeeshVenu

    I am Sibeesh Venu, an engineer by profession and writer by passion. Microsoft MVP, Author, Speaker, Content Creator, Youtuber, Programmer.

    Related articles More from author

    • Code SnippetsJavaScriptJQuery

      Regex to remove a word from a string

      November 2, 2015
      By SibeeshVenu
    • empty_project
      JavaScriptUnit Testing

      Writing JavaScript Tests Using Jasmine Framework

      October 10, 2016
      By SibeeshVenu
    • Chnage_Element_To_Full_Screen_In_Google_Chrome_Error
      JavaScript

      Programmatically Change The Element Or Page Into Full Screen Mode

      March 7, 2016
      By SibeeshVenu
    • JavaScript

      Basic DOM Manipulation in JavaScript

      January 29, 2015
      By SibeeshVenu
    • Javascript_tutorial_output_1
      CodeProjectHow toJavaScript

      Do you know JavaScript? Are you sure? – Part 1

      February 28, 2017
      By SibeeshVenu
    • BlogJavaScript

      Generate Screenshot Using HTML and JavaScript

      December 29, 2014
      By SibeeshVenu
    0

    My book

    Asp Net Core and Azure with Raspberry Pi Sibeesh Venu

    YouTube

    MICROSOFT MVP (2016-2022)

    profile for Sibeesh Venu - Microsoft MVP

    Recent Posts

    • Linux Azure Function Isolated Dot Net 9 YAML Template Deployment
    • Build, Deploy, Configure CI &CD Your Static Website in 5 mins
    • Easily move data from one COSMOS DB to another
    • .NET 8 New and Efficient Way to Check IP is in Given IP Range
    • Async Client IP safelist for Dot NET
    • Post Messages to Microsoft Teams Using Python
    • Get Azure Blob Storage Blob Metadata Using PowerShell
    • Deploy .net 6 App to Azure from Azure DevOps using Pipelines
    • Integrate Azure App Insights in 1 Minute to .Net6 Application
    • Azure DevOps Service Connection with Multiple Azure Resource Group

    Tags

    Achievements (35) Angular (14) Angular 5 (7) Angular JS (15) article (10) Article Of The Day (13) Asp.Net (14) Azure (65) Azure DevOps (10) Azure Function (10) Azure IoT (7) C# (17) c-sharp corner (13) Career Advice (11) chart (11) CSharp (7) CSS (7) CSS3 (6) HighChart (10) How To (9) HTML5 (10) HTML5 Chart (11) Interview (6) IoT (11) Javascript (10) JQuery (82) jquery functions (9) JQWidgets (15) JQX Grid (17) Json (7) Microsoft (8) MVC (20) MVP (9) MXChip (7) News (18) Office 365 (7) Products (10) SQL (20) SQL Server (15) Visual Studio (10) Visual Studio 2017 (7) VS2017 (7) Web API (12) Windows 10 (7) Wordpress (9)
    • .NET
    • Achievements
    • ADO.NET
    • Android
    • Angular
    • Arduino
    • Article Of The Day
    • ASP.NET
    • Asp.Net Core
    • Automobile
    • Awards
    • Azure
    • Azure CDN
    • azure devops
    • Blockchain
    • Blog
    • Browser
    • C-Sharp Corner
    • C#
    • Career Advice
    • Code Snippets
    • CodeProject
    • Cognitive Services
    • Cosmos DB
    • CSS
    • CSS3
    • Data Factory
    • Database
    • Docker
    • Drawings
    • Drill Down Chart
    • English
    • Excel Programming
    • Exporting
    • Facebook
    • Fun
    • Gadgets
    • GitHub
    • GoPro
    • High Map
    • HighChart
    • How to
    • HTML
    • HTML5
    • Ignite UI
    • IIS
    • Interview
    • IoT
    • JavaScript
    • JQuery
    • jQuery UI
    • JQWidgets
    • JQX Grid
    • Json
    • Knockout JS
    • Linux
    • Machine Learning
    • Malayalam
    • Malayalam Poems
    • MDX Query
    • Microsoft
    • Microsoft ADOMD
    • Microsoft MVP
    • Microsoft Office
    • Microsoft Technologies
    • Microsoft Windows
    • Microsoft Windows Server
    • Mobile
    • MongoDB
    • Monthly Winners
    • MVC
    • MVC Grid
    • MySQL
    • News
    • Node JS
    • npm
    • Number Conversions
    • October 2015
    • Office 365
    • Office Development
    • One Plus
    • Outlook
    • Page
    • PHP
    • Poems
    • PowerShell
    • Products
    • Q&A
    • Raspberry PI
    • React
    • SEO
    • SharePoint
    • Skype
    • Social Media
    • Software
    • Spire.Doc
    • Spire.PDF
    • Spire.XLS
    • SQL
    • SQL Server
    • SSAS
    • SSMS
    • Storage In HTML5
    • Stories
    • Third Party Software Apps
    • Tips
    • Tools
    • Translator Text
    • Uncategorized
    • Unit Testing
    • UWP
    • VB.Net
    • Videos
    • Virtual Machine
    • Visual Studio
    • Visual Studio 2017
    • Wamp Server
    • Web API
    • Web Platform Installer
    • Webinars
    • WebMatrix
    • Windows 10
    • Windows 7
    • Windows 8.1
    • Wordpress
    • Writing

    ABOUT ME

    I am Sibeesh Venu, an engineer by profession and writer by passion. Microsoft MVP, Author, Speaker, Content Creator, Youtuber, Programmer. If you would like to know more about me, you can read my story here.

    Contact Me

    • info@sibeeshpassion.com

    Pages

    • About
    • Search
    • Privacy Policy
    • About
    • Search
    • Privacy Policy
    © Copyright Sibeesh Passion 2014-2025. All Rights Reserved.
    Go to mobile version