class Info {
private name: string ;
constructor(n:string){
this.name = n ;
};
describe(){
console.log(`Your name is ${this.name}`);
}
}
const a = new Info('joyous');
a.describe();
class Person{
private name: string;
public constructor(name: string)
{
this.name = name
}
public getName():string{
return this.name;
}
}
var p = new Person("Jane Doe");
console.log(p.getName());
/*this example is more proper than the previous, though the previous example is syntax-wise correct*/
class Animal { public name: string;
public constructor(theName: string) { this.name = theName; }
public move(distanceInMeters: number) { console.log(`${this.name} moved ${distanceInMeters}m.`); }}Try
export class Ingredient{
//This is a shortcut to what is written below
constructor(public name:String, public amount:String){}
}
// On top is the same as below
//
// export class Ingredient{
// public name: String;
// public amount: Number;
// constructor(name:String , amount:Number) {
// this.name = name;
// this.amount = amount;
// }
// }
class Person{
public name: string;
public constructor(name: string)
{
this.name = name
}
}
var p = new Person("Jane Doe");
console.log(p.name);
/*for typescript, you need to not only specify the type in the normall places(e.g. constructor instead of name is name:string), but you also need to specify what the methods are above the constructor*/
/*not necessarily the recommended format for getting name, but at least this is gramatically correct*/
export class Ingredient{
//This is a shortcut to what is written below
constructor(public name:String, public amount:String){}
}
// On top is the same as below
//
// export class Ingredient{
// public name: String;
// public amount: Number;
// constructor(name:String , amount:Number) {
// this.name = name;
// this.amount = amount;
// }
// }
class Person{
private name: string;
public constructor(name: string)
{
this.name = name
}
public getName():string{
return this.name;
}
}
var p = new Person("Jane Doe");
console.log(p.getName());
/*this example is more proper than the previous, though the previous example is syntax-wise correct*/
class Person{
public name: string;
public constructor(name: string)
{
this.name = name
}
}
var p = new Person("Jane Doe");
console.log(p.name);
/*for typescript, you need to not only specify the type in the normall places(e.g. constructor instead of name is name:string), but you also need to specify what the methods are above the constructor*/
/*not necessarily the recommended format for getting name, but at least this is gramatically correct*/