OOP Javascript - Is “get property” method necessary?

Given a very simple js object constructor and its prototype...

function MyTest(name) { this.name = name; } MyTest.prototype = { getName: function() { var myName = this.name; return myName; }, myMethod: function() { // } }

Now, in myMethod, is there any difference between using "this.getName" and "this.name"?



Depends on whether name is an object nor a primitive (boolean, string, int).

If name is a primitive/ string, and you use this.getName(), you won't be able to modify the value of this.name by doing this (assuming this.name = "Billy")

this.getName() = "213" //this.name still has a value of "Billy"

If name is an object this.name = { firstName: "Billy", lastName: "Bob"};

this.getName().firstName = "Willy"; //this.name now has a value of { firstName: "Willy", lastName: "Bob"}

This is due to JavaScript passing primitives by value, but passing objects by reference.


Using the function is slightly slower, but allows you to change how it works in the future (or to do so in another object type that inherits from this one).


There is no difference. This is not how you implement private vars in JS if that is what you want.


  • When to use .length vs .length() [duplicate]
  • SQL: How to get relational data from two tables? (Using JOIN ?)
  • SQL Query (or Join) for 3 tables
  • Stop PhpStorm from adding a new line after function declaration when arguments split across multiple
  • SQL Group BY using strings in new columns
  • Can I include a Generic type parameter in a lamba expression? (VB.NET 2010)
  • In Perl, how do I ensure that a sub is invoked as a method from methods of the same object?
  • Is there a need to prepend setTimeout and setInterval with window object?
  • Gson serializing Spring beans
  • how to save bool value in KeychainItemWrapper
  • Yii2: Using Kartik Depdrop Widget?
  • c++ using primitive types as a base class
  • How do you keep a running instance for Google App Engine
  • Scala: Function returning an unknown type
  • What is the use of a session store?
  • Using MouseListener to select a range of cells in a grid
  • How to copy styled text in JTextPane
  • How to explicitly/implicitly implemented interface members in C++/CLI?
  • Jquery Knockout: ko.computed() vs classic function?
  • How to autopopulate a field in SugarCRM form
  • Java color detection
  • System.InvalidCastException: Specified cast is not valid
  • zope_i18n_compile_mo_files doesn't work on a Zeo configuration
  • How can I extract results of aggregate queries in slick?
  • Cancel a live stream “fast motion” catch-up in Flash
  • Scrapy recursive link crawler
  • Illegal mix of collations for operation for date/time comparison
  • Master page gives error
  • PHP - How to update data to MySQL when click a radio button
  • Redux, normalised entities and lodash merge
  • Rearranging Cells in UITableView Bug & Saving Changes
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • How do you join a server to an Active Directory (domain)?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • apache spark aggregate function using min value
  • Bitwise OR returns boolean when one of operands is nil
  • Sorting a 2D array using the second column C++
  • java string with new operator and a literal