0

Trying to make a static function give access to a property xdate, which was initialized in the constructor (in typescript 1.5.3).

this.xdate is accessible in all other instance methods. it remains inaccessible in the static method.

Q. Is there anyway I can make this.xdate accessible in the static method?

Below is my code:-

class vehicle{
  constructor(private xdate = new Date()){}

  vroom(){    console.log(this.xdate);  }
  static staticvroom(){ console.log(this.xdate);} //Warning: Property 'xdate' does not exist on type 'typeof vehicle'.


}//vehicle ends

let v1 = new vehicle;
v1.vroom(); //works

vehicle.staticvroom(); //undefined

/* this is how i executed it:- 
D:\js\Ang2\proj2\myforms\tsc_cRAP>tsc 7_.ts
7_.ts(18,42): error TS2339: Property 'xdate' does not exist on type 'typeof vehicle'.

D:\js\Ang2\proj2\myforms\tsc_cRAP>node 7_.js
2017-06-23T09:17:41.365Z
undefined

*/

Any pointers would be of great help. (If this is a repeated question, apologies in advance)

/* UglyHack#1: since static methods are available even before the instance of an object, I created a tmp obj. within staticvroom(){} and it worked.

static staticvroom(){ 
  console.log((new vehicle).xdate); 
} 

Not sure the performance issues on this.

*/

8
  • 1
    You cannot. Since this inside static members refers to the class itself instead of the class instance. Commented Jun 23, 2017 at 9:29
  • I can create a new obj. within the static method and then access it using the obj variable. this worked for me: ` static staticvroom(){ let x = new vehicle; console.log(x.xdate); } ` Commented Jun 23, 2017 at 10:13
  • 1
    That's a horrible design, and it's extremely error prone. Commented Jun 23, 2017 at 10:20
  • Can you explain why is it horrible? I can set the object to null and delete all references to it towards the end in staticvroom(). static methods are accessible even before an instance is created; just might use that functionality, i thought. though i know its ugly, but i dont see any performance issues. Commented Jun 23, 2017 at 10:27
  • 1
    Static methods can not access any non-static parts of that class' instances by design. It seems like you should either not have that method static or not have xdate as a non-static field. However, we cannot be sure of this unless you improve your question with your main intents. Commented Jun 23, 2017 at 10:32

1 Answer 1

4

You need to pass instance as parameter

class Vehicle {
  constructor(private xdate = new Date()) {}
  vroom(){
    console.log(this.xdate);
  }
  static staticVar: Date = new Date();
  static staticvroom(vehicle: Vehicle) {
    console.log(vehicle.xdate);
    console.log(this.staticVar); // You can even access static vars with this
  }


}// vehicle ends

let v1 = new Vehicle;
v1.vroom(); // works

Vehicle.staticvroom(v1);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.