0
class Person {
   constructor() {
    let name = 'John';
  }
  talk() {
    return "talking"
 }
}
const me = new Person();
Person.prototype.walk = function walk() { return "walking"; };

as you can see in the image below, why I am getting undefined for me.name (although it was present in the constructor)?

Why accessible through Person and why not with me?

Do we get only the prototype object properties to be accessed to me, or do we get constructor properties? If not then what's the use of passing them by default to the me instance as you can see in the image below?

enter image description here

lucky
  • 31
  • 8
  • 1
    a) you've spelled it `fname`, not [`name`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name), so they're clearly not the same b) you've declared a local variable with `let`, not [created a property by assigning to `this.name`](https://stackoverflow.com/q/13418669/1048572?javascript-do-i-need-to-put-this-var-for-every-variable-in-an-object) – Bergi Sep 11 '22 at 12:17
  • what if I use a var in this case ? – lucky Sep 12 '22 at 05:04
  • No difference, that introduces a variable in the constructor scope. You must create a property on the object. – Bergi Sep 12 '22 at 11:19

2 Answers2

0

MDN docs:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes

Does not how JavaScript classes work.
You should check the docs.
To define the constructor you have to define the constructor function inside the class, which is named constructor.

class Person {

  constructor(name = 'John') {
    this.name = name
  }

  talk() {
    return 'talking'
  }

}
Xion 14
  • 427
  • 2
  • 8
  • even thought , I changed to constructor , it still same , this.name will be copied to instance by not name , y ? – lucky Sep 11 '22 at 16:26
0

let declares a local variable (one which disappears when the } bracket is closed). You need to store your variable as part of the class.

From the MDN example:

class Polygon {
  constructor(height, width) {
    this.area = height * width;
  }
}

console.log(new Polygon(4, 3).area);
// expected output: 12
Codebling
  • 10,764
  • 2
  • 38
  • 66
  • is that only this. variables and methods can only be accessed by instances and not all the general variables and methods , is that right ? – lucky Sep 12 '22 at 05:07
  • and I had another question too ,, in the above example you declared area right , usually we declare variables with let , const and var , how are we creating a variable with this ? – lucky Sep 12 '22 at 05:10
  • @lucky second question first. You can create a field in any object by assigning it. e.g. `a={}; a.name=5;` The first statement creates an empty object `a`, the second one adds a field called `name` to it. Play around in the REPL until you figure it out. – Codebling Sep 12 '22 at 05:34
  • 1
    Use `let` and `const`. Don't use `var`. (You can also declare a variable without one of those keywords, but behind the scenes it becomes part of `window` in the browser or `global` in node. Javascript is very weird in that way - without one of `let`, `const` or `var`, you're creating an field on an existing object, not declaring a new variable). A *variable* is its own unique storage and will get garbage collected (cleared from memory) when it is no longer needed. A *field* is a property of another object and will stay in memory until the object it is attached to is garbage collected. – Codebling Sep 12 '22 at 05:39
  • 1
    @lucky I hope that makes sense. `this` is just another object (which happens to reference the instance of a class or a function), that's why `this.name=5` creates `name` on `this` (it creates a *field* or *property* of `this). It doesn't declare a variable. You are right that you'd need a keyword to create a variable. – Codebling Sep 12 '22 at 05:42