FrontendDeveloper.in

JavaScript Interview Questions

Filter by topic:
  • The constructor method is a special method for creating and initializing an object created within a class. If you do not specify a constructor method, a default constructor is used. The example usage of constructor would be as below,

    class Employee {
    constructor() {
    this.name = "John";
    }
    }
    
    var employeeObject = new Employee();
    
    console.log(employeeObject.name); // John
    
  • The "constructor" in a class is a special method and it should be defined only once in a class. i.e, If you write a constructor method more than once in a class it will throw a SyntaxError error.

    class Employee {
    constructor() {
    this.name = "John";
    }
    constructor() {   //  Uncaught SyntaxError: A class may only have one constructor
    this.age = 30;
    }
    }
    
    var employeeObject = new Employee();
    
    console.log(employeeObject.name);
    

    This constructor is called by using the special function call new (see example above).

  • You can use the super keyword to call the constructor of a parent class. Remember that super() must be called before using this reference. Otherwise it will cause a reference error. Let's the usage of it,

    class Square extends Rectangle {
    constructor(length) {
    super(length, length);
    this.name = "Square";
    }
    
    get area() {
    return this.width * this.height;
    }
    
    set area(value) {
    this.area = value;
    }
    }
    
  • You can use the Object.getPrototypeOf(obj) method to return the prototype of the specified object. i.e. The value of the internal prototype property. If there are no inherited properties then null value is returned.

    const newPrototype = {};
    const newObject = Object.create(newPrototype);
    
    console.log(Object.getPrototypeOf(newObject) === newPrototype); // true
    
  • In ES5, it will throw a TypeError exception if the obj parameter isn't an object. Whereas in ES2015, the parameter will be coerced to an Object.

    // ES5
    Object.getPrototypeOf("James"); // TypeError: "James" is not an object
    // ES2015
    Object.getPrototypeOf("James"); // String.prototype
    
  • You can use the Object.setPrototypeOf() method that sets the prototype (i.e., the internal Prototype property) of a specified object to another object or null. For example, if you want to set prototype of a square object to rectangle object would be as follows,

    Object.setPrototypeOf(Square.prototype, Rectangle.prototype);
    Object.setPrototypeOf({}, null);
    
  • The Object.isExtensible() method is used to determine if an object is extendable or not. i.e, Whether it can have new properties added to it or not.

    const newObject = {};
    console.log(Object.isExtensible(newObject)); //true
    

    Note: By default, all the objects are extendable. i.e, The new properties can be added or modified.

  • The Object.preventExtensions() method is used to prevent new properties from ever being added to an object. In other words, it prevents future extensions to the object. Let's see the usage of this property,

    const newObject = {};
    Object.preventExtensions(newObject); // NOT extendable
    
    try {
    Object.defineProperty(newObject, "newProperty", {
    // Adding new property
    value: 100,
    });
    } catch (e) {
    console.log(e); // TypeError: Cannot define property newProperty, object is not extensible
    }
    
  • You can mark an object non-extensible in 3 ways,

    1. Object.preventExtensions
    2. Object.seal
    3. Object.freeze
    var newObject = {};
    
    Object.preventExtensions(newObject); // Prevent objects are non-extensible
    Object.isExtensible(newObject); // false
    
    var sealedObject = Object.seal({}); // Sealed objects are non-extensible
    Object.isExtensible(sealedObject); // false
    
    var frozenObject = Object.freeze({}); // Frozen objects are non-extensible
    Object.isExtensible(frozenObject); // false
    
  • The Object.defineProperties() method is used to define new or modify existing properties directly on an object and returning the object. Let's define multiple properties on an empty object,

    const newObject = {};
    
    Object.defineProperties(newObject, {
    newProperty1: {
    value: "John",
    writable: true,
    },
    newProperty2: {},
    });
    
  • The MEAN (MongoDB, Express, AngularJS, and Node.js) stack is the most popular open-source JavaScript software tech stack available for building dynamic web apps where you can write both the server-side and client-side halves of the web project entirely in JavaScript.

  • Obfuscation is the deliberate act of creating obfuscated javascript code(i.e, source or machine code) that is difficult for humans to understand. It is something similar to encryption, but a machine can understand the code and execute it. Let's see the below function before Obfuscation,

    function greeting() {
    console.log("Hello, welcome to JS world");
    }
    

    And after the code Obfuscation, it would be appeared as below,

    eval(
    (function (p, a, c, k, e, d) {
    e = function (c) {
    return c;
    };
    if (!"".replace(/^/, String)) {
    while (c--) {
    d[c] = k[c] || c;
    }
    k = [
    function (e) {
    return d[e];
    },
    ];
    e = function () {
    return "\\w+";
    };
    c = 1;
    }
    while (c--) {
    if (k[c]) {
    p = p.replace(new RegExp("\\b" + e(c) + "\\b", "g"), k[c]);
    }
    }
    return p;
    })(
    "2 1(){0.3('4, 7 6 5 8')}",
    9,
    9,
    "console|greeting|function|log|Hello|JS|to|welcome|world".split("|"),
    0,
    {}
    )
    );
    
  • Below are the few reasons for Obfuscation,

    1. The Code size will be reduced. So data transfers between server and client will be fast.
    2. It hides the business logic from outside world and protects the code from others
    3. Reverse engineering is highly difficult
    4. The download time will be reduced
  • Minification is the process of removing all unnecessary characters(empty spaces are removed) and variables will be renamed without changing it's functionality. It is also a type of obfuscation .

  • Normally it is recommended to use minification for heavy traffic and intensive requirements of resources. It reduces file sizes with below benefits,

    1. Decreases loading times of a web page
    2. Saves bandwidth usages