Chapter 8 - The this Keyword I


#1

I came across this while learning JavaScript

Hello, my name is ${name here} … i failed after several attempts trying to decipher what that symbol was!

it is not a single quotation mark by the way and i didn’t know one can code with. After several hours, i just found it on my keyboard. Just incase anyone is having the same issue, It is not a single quotation mark!

Happy coding!


8.15 - The This Keyword! (Yes ... another one!)
#2

They are called backticks.

`Hello, my name is ${name here}`

#3

What you encountered was a template string which is the ES6 method for interpolating data into a string for storage or output. The data may be any expression or variable, which in the above case would be, this.name.

Glad that you took the time to find the backtick, and also to discover that it is useful for inline formatting, such as your opening post, and also, when three are used before and after, for posting code blocks.

function Person(name, isMarried) {
    this.name = name;
    this.isMarried = isMarried;
    this.getName = function () {
        return `Hello, my name is ${this.name}.`;
    };
    this.getStatus = function () {
        return `${this.name} is${this.isMarried ? '' : ' not'} married.`;
    };
}

const weegillis = new Person("Wee Gillis", true);
const peterpan = new Person("Peter Pan", false);

console.log(weegillis.getName());    //->  Hello, my name is Wee Gillis.
console.log(peterpan.getName());     //->  Hello, my name is Peter Pan.

console.log(weegillis.getStatus());  //->  Wee Gillis is married.
console.log(peterpan.getStatus());   //->  Peter Pan is not married.

#4

Thanks, sounds cool.


#5

Thanks, this is so cool an explanation and use!


#6

When you say inline, what do you mean? inline to html or to what/where?


#7

inline refers to in the flow of prose text.

Where you wrote, 'Hello, my name is ${name here} ', it is in the same line as the rest of the sentence, marked down with single backticks.

`Hello, ...`

#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.