0
class Student {
    fullname: string;
    constructor(public lastName: string, public middleName: string, public firstName: string) {
        this.fullname = firstName + ", " + middleName + ", " + lastName;
    }
}
interface Person {
    lastName: string; middleName: string; firstName: string;
}
function greeter(person: Person) {
    return "Hello, " + person.firstName + ", " + person.middleName + ", " + person.lastName;
}
let theName = new Student("first name", "Mid", "Last");
console.log(greeter(theName)) //; the result is : Hello, Last, Mid, first name what is happening?

hello, just wondering if the string order inside the classes and functions is enforced strictly or is it just bugs?

3
  • 2
    Don't post images, post code. Also it's not very clear what the question is Commented Aug 6, 2018 at 12:38
  • Not in comments .. in the question ! I added it. Commented Aug 6, 2018 at 12:46
  • The order in your constructor does not match the values you pass in. In the constructor you define the order as lastName, middleName and firstName, while you call it with "first name", "middle name" and "last name". Commented Aug 6, 2018 at 12:57

1 Answer 1

1

A string is a string the compiler can't know what their meaning is (such as this string is a first name and this other one is a last name (well actually there is something but it's a pretty esoteric application of branded types not going to get into it here)).

You pass in the string "first name" as the value for the lastName parameter, and "Last" as the value for the firstName parameter. The result is consistent with the values you pass in. When you invoke constructors and functions the orders dictates what argument value goes in which parameter.

You can change the parameter order to a more logical order:

class Student {
    constructor(public firstName: string, public middleName: string, public lastName: string) {
    }
}
interface Person {
    lastName: string; middleName: string; firstName: string;
}
function greeter(person: Person) {
    return "Hello, " + person.firstName + ", " + person.middleName + ", " + person.lastName;
}
let theName = new Student("first name", "Mid", "Last");
console.log(greeter(theName)) 

Or you can take in an object literal that more explicitly states what each string means:

class Student {
    lastName: string; middleName: string; firstName: string;
    constructor(data: Person) {
        this.lastName = data.lastName;
        this.middleName = data.middleName;
        this.firstName = data.firstName;
    }
}
interface Person {
    lastName: string; middleName: string; firstName: string;
}
function greeter(person: Person) {
    return "Hello, " + person.firstName + ", " + person.middleName + ", " + person.lastName;
}
let theName = new Student({ // We state each string as a property of an object literal
    firstName: "first name",
    middleName: "Mid",
    lastName: "Last"
});
console.log(greeter(theName)) 

Note I removed the fullName field as it was not really being use anywhere (the greeter function does not use it). You can add that as needed but I did not see a reason to include an unused field in the example.

Sign up to request clarification or add additional context in comments.

2 Comments

maybe it's me, let me explain just a little tiny bit more inside the constructor there is this (public one:string, public two:string, public three string) now, if i execute the code it will result in--> one two three but if i shuffle the order and do this-->(public three:string, public one:string, two:string) then the result is--> three one two why is it following the order in which i place variables inside a constructor or inside the function likewise function strings(stringss:Strings){return stringss.two + " " + one + " " + three} the result is two one three
@user2699595 Like I say in the answer, order matters for the constructor, when you invoke the constructor the arguments are passed in positionally (new Student("first name", "Mid", "Last")) so changing the order of parameters changes what that call means.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.