Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7193087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:05:52+00:00 2026-05-28T20:05:52+00:00

Take this code: var obj = { init: function(){ console.log(obj.count); //or console.log(this.count); }, count:

  • 0

Take this code:

var obj = {
  init: function(){
    console.log(obj.count);
    //or 
    console.log(this.count);
  },
  count: 1,
  msg: 'hello'
}

obj.init();

I can access property of obj by this or the variable name obj both. Is there any advantage in using this ? Because in my opinion using the object name obj adds clarity to the code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T20:05:53+00:00Added an answer on May 28, 2026 at 8:05 pm

    The advantage of this is that it allows the same function to work for multiple instances of that type of object. The corresponding disadvantage of using the variable name obj is that it only refers to that specific instance.

    In your case you’ve only got a singleton object since you assign your obj variable to an object literal, so there won’t ever be multiple instances, but still if you later copied that code to create another similar object with a different variable name for that new object you’d have to find/replace all the uses of obj and change them to the new variable name.

    Note that in JavaScript the value of this within a function depends on how the function was called, not whether the function was defined as a property/method of an object. See https://developer.mozilla.org/en/JavaScript/Reference/Operators/this.

    As far as making the code readable, this is a standard part of the language that all experienced JS coders are (or should be) familiar with, and in my opinion it is more readable because you don’t have to look back to see where obj was declared…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I take this code <script> var arr = [<%= myArray %>+<%= my2Array
For instance, take this piece of code: var person = new Person(); or for
take a look at this code: $(document).ready(function() { document.getElementById(sliderId).onmousedown = sliderMouseDown; }); function sliderMouseDown()
Take a look at the following code: var o; (function (p) { p =
Take the following code as a sample: procedure TForm1.Button1Click(Sender: TObject); var Obj: TSomeObject; begin
Consider the following code (you can just put this in the developer console in
Take this code: class Register(var value:Int = 0) { def getZeroFlag() : Boolean =
Take this code: struct mystruct { int var; mystruct() : var(0) {} }; int
Take this code: <?php if (isset($_POST['action']) && !empty($_POST['action'])) { $action = $_POST['action']; } if
Take this code into consideration: Proxy p = new Proxy(Type.SOCKS, new InetSocketAddress(proxyURL, port)); try

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.