use es5 in javascript

ES5, or ECMAScript 5, is the fifth edition of the ECMAScript standard, which is the set of specifications that JavaScript follows. Here are some key features of ES5 and how to use them in JavaScript:

  • Variables: In ES5, you can declare variables using the var keyword. It's important to note that var has function scope, which means that it's only accessible within the function it was declared in. Here's an example:
index.tsx
function foo() {
  var x = 1;
  console.log(x);
}

foo(); // Output: 1
console.log(x); // Output: ReferenceError: x is not defined
131 chars
8 lines
  • Functions: In ES5, you can declare functions using the function keyword. One important feature of ES5 functions is that they have a this keyword that refers to the object that they're attached to. Here's an example:
index.tsx
var person = {
  name: 'John',
  sayName: function() {
    console.log(this.name);
  }
};

person.sayName(); // Output: John
125 chars
9 lines
  • Syntax: In ES5, there are a few changes to the syntax of JavaScript. For example, you can create multi-line strings using backticks (`). Here's an example:
index.tsx
var message = `This is a 
multi-line string`;

console.log(message); // Output: This is a
                      //         multi-line string
141 chars
6 lines
  • Object methods: In ES5, you can add methods to objects using object literals. Here's an example:
index.tsx
var person = {
  name: 'John',
  sayName: function() {
    console.log(this.name);
  }
};

person.sayHello = function() {
  console.log('Hello, my name is ' + this.name);
};

person.sayHello(); // Output: Hello, my name is John
228 chars
13 lines
  • Prototypes: In ES5, you can use prototype-based inheritance to add methods to existing objects. Here's an example:
index.tsx
function Person(name) {
  this.name = name;
}

Person.prototype.sayName = function() {
  console.log(this.name);
};

var john = new Person('John');
john.sayName(); // Output: John
180 chars
11 lines

gistlibby LogSnag