0

For example, I am making a Matrix constructor that'll allow you to perform various calculations, such as matrix multiplication etc:

var Matrix = function(m = []){this.matrix = m; ...};

obviously the main property is the matrix property...is it possible to make it so that if I declare var mat1 = new Matrix([...]); that when I call mat1, it returns the matrix property automatically, unless another property is called?

Tara
  • 389
  • 3
  • 14
  • 3
    what do you mean by "when I call mat1"? – le_m Jan 23 '18 at 17:24
  • 1
    If you mean "whenever I mention `mat1` in any context it's interpreted to mean `mat1.matrix`": No. And you wouldn't want that, it would be breaking assumptions and behaviour left and right. – deceze Jan 23 '18 at 17:28
  • Question doesn't really make sense as written – charlietfl Jan 23 '18 at 17:28
  • so, for example, i could declare firstElement = mat1[0][0] and it would be the same as mat1.matrix[0][0], and make it so that mat1 inherits the Array properties and when called are automatically applied to Matrix.matrix (so mat1.length would be the same as mat1.Matrix.length, and mat1.pop() would be the same as mat1.Matrix.pop() etc... – Tara Jan 23 '18 at 17:32
  • 1
    You *could* hack something together using ES6 proxies, but I wouldn't recommend it. – le_m Jan 23 '18 at 17:41

1 Answers1

0

If you're saying you want to be able to access this.matrix when you reference mat1, no, that's not how the constructor function works. When you use the constructor, you get the value of this returned. This answer: https://stackoverflow.com/a/3350307/1449156 has some good details on how the constructor works, and why that happens.

Pam
  • 109
  • 3
  • mostly, I just want to be able to use the syntax mat1[0][0] and have it mean mat1.matrix[0][0], or it least have it return that. – Tara Jan 23 '18 at 17:38
  • `[]` syntax is for accessing array index or object properties, you still need to call `.matrix` to get what you want – XPX-Gloom Jan 23 '18 at 17:49