Javascript: Do I need to put this.var for every variable in an object?

Asked
Active3 hr before
Viewed126 times

9 Answers

objectjavascript
90%

GitLab launches Collective on Stack Overflow ,Thanks for contributing an answer to Stack Overflow!, Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers , Stack Overflow Public questions & answers

Example:

function Foo() {
   this.bar = "foo"; // creating a property on the instance
}
Foo.prototype.foo = 0; // of course you also can define other values to inherit
Foo.prototype.getBar = function() {
   // quite useless
   return this.bar;
}

var foo = new Foo; // creates an object which inherits from Foo.prototype,
// applies the Foo constructor on it and assigns it to the var
foo.getBar(); // "foo" - the inherited function is applied on the object and
// returns its "bar" property
foo.bar; // "foo" - we could have done this easier.
foo[foo.bar]; // 0 - access the "foo" property, which is inherited
foo.foo = 1; // and now overwrite it by creating an own property of foo
foo[foo.getBar()]; // 1 - gets the overwritten property value. Notice that
(new Foo).foo; // is still 0

Let's see it in action:

function Foo() {
   var bar = "foo"; // a local variable
   this.getBar = function getter() {
      return bar; // accesses the local variable
   }; // the assignment to a property makes it available to outside
}

var foo = new Foo; // an object with one method, inheriting from a [currently] empty prototype
foo.getBar(); // "foo" - receives us the value of the "bar" variable in the constructor

Notice that the methods on the prototype object do not have access to the local variables of the constructor, yet they might use the privileged methods. Let's add one:

Foo.prototype.getFooBar = function() {
   return this.getBar() + "bar"; // access the "getBar" function on "this" instance
}
// the inheritance is dynamic, so we can use it on our existing foo object
foo.getFooBar(); // "foobar" - concatenated the "bar" value with a custom suffix
load more v
88%

Alternatively, the Destructuring Assignment syntax can also be used to declare variables.,ReferenceError: assignment to undeclared variable "x", The var statement declares a function-scoped or globally-scoped variable, optionally initializing it to a value. ,SyntaxError: redeclaration of formal parameter "x"

var varname1[ = value1][, varname2[ = value2]...[, varnameN[ = valueN]]];
load more v
72%

I now use the keywords const and let to declare all my variables in JavaScript and so should you.,If you write code like var x = 5 you need to stop. Well, I’ll be honest, you don’t have to, but you should.,We all start somewhere. I learned JavaScript declaring variables with the var keyword. It was simple and it worked, but I’ve changed.,To use the const keyword, simply swap it out with var and now that value cannot be modified.

To use the const keyword, simply swap it out with var and now that value cannot be modified.

// the old way// var sales_tax = 0.06;// the new wayconst sales_tax = 0.06;sales_tax = 0.08; // "TypeError: Assignment to constant variable.
load more v
65%

In programming, just like in algebra, we use variables (like price1) to hold values.,The variable carName will have the value undefined after the execution of this statement:,In programming, just like in algebra, we use variables in expressions (total = price1 + price2).,The variable carName will still have the value "Volvo" after the execution of these statements:

var = "";
75%

The following code fragment illustrates some of the differences between truly undefined and merely unassigned variables:,After this code runs, the original string “hello” is no longer reachable; there are no references to it in any variables in the program. The system detects this fact and frees up its storage space for reuse.,Function definitions can be nested. Each function has its own local scope, so it is possible to have several nested layers of local scope. For example:,[*] This is merely an aside; if it does not interest you, feel free to move on to the next section.

i = 10;
i = "ten";
load more v
40%

and we can also access those same variables within other functions:,Declaring a variable in JavaScript has always traditionally been done with the var keyword.,We can also declare a variable inside of a function:,Variable DeclarationsHow to create and type JavaScript variables

Declaring a variable in JavaScript has always traditionally been done with the var keyword.

tsvar a = 10;
load more v
22%

Use the reserved keyword var to declare a variable in JavaScript. ,Above, the var msg; is a variable declaration. It does not have any value yet. The default value of variables that do not have any value is undefined., JavaScript allows multiple white spaces and line breaks when you declare a variable with var keyword. ,A variable name cannot be a reserved keyword in JavaScript, e.g. var, function, return cannot be variable names.

var <variable-name>;

   var <variable-name> = <value>;
load more v
60%

To declare a variable, you use the var keyword followed by the variable name as follows:,You can declare two or more variables using one statement, each variable declaration is separated by a comma (,) as follows:,After declaring a variable, you can assign the variable a string as follows:,To declare a variable and initialize it at the same time, you use the following syntax:

To declare a variable, you use the var keyword followed by the variable name as follows:

.wp - block - code {
      border: 0;
      padding: 0;
   }

   .wp - block - code > div {
      overflow: auto;
   }

   .shcb - language {
      border: 0;
      clip: rect(1 px, 1 px, 1 px, 1 px); -
      webkit - clip - path: inset(50 % );
      clip - path: inset(50 % );
      height: 1 px;
      margin: -1 px;
      overflow: hidden;
      padding: 0;
      position: absolute;
      width: 1 px;
      word - wrap: normal;
      word - break: normal;
   }

   .hljs {
      box - sizing: border - box;
   }

   .hljs.shcb - code - table {
      display: table;
      width: 100 % ;
   }

   .hljs.shcb - code - table > .shcb - loc {
      color: inherit;
      display: table - row;
      width: 100 % ;
   }

   .hljs.shcb - code - table.shcb - loc > span {
      display: table - cell;
   }

   .wp - block - code code.hljs: not(.shcb - wrap - lines) {
      white - space: pre;
   }

   .wp - block - code code.hljs.shcb - wrap - lines {
      white - space: pre - wrap;
   }

   .hljs.shcb - line - numbers {
      border - spacing: 0;
      counter - reset: line;
   }

   .hljs.shcb - line - numbers > .shcb - loc {
      counter - increment: line;
   }

   .hljs.shcb - line - numbers.shcb - loc > span {
      padding - left: 0.75 em;
   }

   .hljs.shcb - line - numbers.shcb - loc::before {
      border - right: 1 px solid #ddd;
      content: counter(line);
      display: table - cell;
      padding: 0 0.75 em;
      text - align: right; -
      webkit - user - select: none; -
      moz - user - select: none; -
      ms - user - select: none;
      user - select: none;
      white - space: nowrap;
      width: 1 % ;
   }
var message;
Code language: JavaScript(javascript)
load more v
48%

The variable this represents one of the main headaches for developers starting to learn Javascript. Unlike other languages where its value is very intuitive, in Javascript it is necessary to know a series of rules in order to understand the value that this variable will finally adopt.

The variable this represents one of the main headaches for developers starting to learn Javascript. Unlike other languages where its value is very intuitive, in Javascript it is necessary to know a series of rules in order to understand the value that this variable will finally adopt.

this

Other "object-javascript" queries related to "Javascript: Do I need to put this.var for every variable in an object?"