FrontendDeveloper.in

JavaScript Interview Questions

  • Question 256

    What is a constructor method

    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
    
  • Question 257

    What happens if you write constructor more than once in a class

    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).

  • Question 258

    How do you call the constructor of a parent class

    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;
    }
    }
    
  • Question 259

    How do you get the prototype of an object

    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
    
  • Question 260

    What happens If I pass string type for getPrototype method

    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
    
  • Question 261

    How do you set the prototype of one object to another

    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);
    
  • Question 262

    How do you check whether an object can be extended or not

    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.

  • Question 263

    How do you prevent an object from being extend

    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
    }
    
  • Question 264

    What are the different ways to make an object non-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
    
  • Question 265

    How do you define multiple properties on an object

    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: {},
    });
    
  • Question 266

    What is the MEAN stack

    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.

  • Question 267

    What is obfuscation 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,
    {}
    )
    );
    
  • Question 268

    Why do you need Obfuscation

    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
  • Question 269

    What is Minification

    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 .

  • Question 270

    What are the advantages of minification

    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
Get LinkedIn Premium at Rs 399