Some time back I had written article on Introduction of TypeScript. We already know that OOPS concept can be used on JavaScript, but with TypeScript we can achieve in much simpler way.
In this particular article we will check interaction of object in different way to handle HTML interaction situations in cleaner way with the help of objects and functions.
We will check out by creating TS interface and passing it to some other function which will change some of the object properties and same can be communicated back to the source.
I am trying to keep things simple only by using simple approach
Let's consume the above to other class which can be initialized be calling constructor. It is having simple method which is defining the SpeedChange body.
The same option object can be stored and send across any other function. By calling SpeedChange the logging would be done from Main class.
Ex:
The example is simple but can be very useful in real world client side driven development.
NOTE: The objects can be stored as JS variable as well and can be referred to somewhere else with the help of window object. Out of OOPS and flexibility of JS :)
In this particular article we will check interaction of object in different way to handle HTML interaction situations in cleaner way with the help of objects and functions.
We will check out by creating TS interface and passing it to some other function which will change some of the object properties and same can be communicated back to the source.
I am trying to keep things simple only by using simple approach
interface ICar {
Model: string
Speed: number;
SpeedChange: (speed:number) => void;
}
Let's consume the above to other class which can be initialized be calling constructor. It is having simple method which is defining the SpeedChange body.
class Main {
constructor(private option: ICar) {
option.SpeedChange = (speed) => {
console.log(speed);
};
}
}
The same option object can be stored and send across any other function. By calling SpeedChange the logging would be done from Main class.
Ex:
public static SomeOtherClassFunction(car: ICar) {
car.SpeedChange(20);
}
The example is simple but can be very useful in real world client side driven development.
NOTE: The objects can be stored as JS variable as well and can be referred to somewhere else with the help of window object. Out of OOPS and flexibility of JS :)
Comments
Post a Comment