Creating constructors using "this" instead of simply returning an object

Tanckom

My knowledge of words might not be sufficient to find this by myself the explanation on the www. So please excuse me if this might be a duplicate.

I'm currently trying to figure out why we use "this" in a function constructor instead of simply returning an object?

E.g. this JSFiddle

// Using this inside function
function Student1(first,last) {
    this.firstName = first;
  this.lastName = last;
  this.display = function(){
    return this.firstName + " " + this.lastName;
  };
}

const harry = new Student1("Harry", "Potter");

document.querySelector("div").innerHTML = harry.display();

document.querySelector("div").innerHTML += "<br>";


// Returning object
function Studen2(first,last){
    return {
    firstName: first,
    lastName: last,
    display(){
        return this.firstName + " " + this.lastName;
    }
  };
}

const ron = new Student1("Ron", "Weasley");

document.querySelector("div").innerHTML += ron.display();

Anyone mind to explain or guide me to the right direction?

Bart van den Burg

Generally you will want to define the object methods on the class prototype, so that they don't have to be reinstantiated every time you create a new instance of the class, e.g.:

function Student1(first,last) {
    this.firstName = first;
    this.lastName = last;
}

Student1.prototype.display = function() {
    return this.firstName + " " + this.lastName;
}

const harry = new Student1("Harry", "Potter");

document.querySelector("div").innerHTML = harry.display();

If you just return an (anonymous) object, it won't have a prototype and you'll have to define the function every time the construction function gets called.

Also, in your example:

harry instanceof Student1 // true
ron  instanceof Student2 // false

So you can't use instanceof.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

When would you use object BigInteger instead of simply using double?

From Dev

using .map() instead of .forEach for creating an object

From Dev

Returning Future<Object> instead of Object

From Java

what is the difference in using InputStream instead of FileInputStream while creating the FileInputStream object

From Dev

Constructors in c++ making an object with [] instead of brackets

From Dev

I need help creating constructors and returning boolean and strings

From Dev

Returning PromiseValue when creating an object

From Java

Object instantiation using different Constructors

From Dev

Why is it returning address instead of object?

From Dev

Promise returning a function instead of an object

From Dev

Laravel returning "Object" instead of Array?

From Dev

Console returning [Object object] instead of object members

From Dev

Object returning null after creating object

From PHP

Returning a object gets true instead of beeing an object

From Dev

Problem with Python returning a NoneType object instead of the Object

From Dev

Creating a User object instead of an NSEntityDescription object

From Dev

No Visible constructors for class when creating Java object in Jython

From Dev

Creating json object in mvc and returning from controller

From Dev

Using Delphi object constructors on preallocated memory

From Dev

Using an object in a single class with multiple constructors

From Dev

Using multiple constructors with one object in Java

From Java

Thymeleaf returning a String array instead of List<Object>

From Javascript

javascript push returning number instead of object

From Dev

jwplayer returning function instead of player object

From Python

Django HttpResponse returning object instead of stated string

From Dev

Linq select returning string instead of object

From Dev

NGRX 8 reducer returning Object instead of Array

From Dev

simple string split is returning object instead array

From Dev

Python returning Hexadecimal address instead of object

Related Related

  1. 1

    When would you use object BigInteger instead of simply using double?

  2. 2

    using .map() instead of .forEach for creating an object

  3. 3

    Returning Future<Object> instead of Object

  4. 4

    what is the difference in using InputStream instead of FileInputStream while creating the FileInputStream object

  5. 5

    Constructors in c++ making an object with [] instead of brackets

  6. 6

    I need help creating constructors and returning boolean and strings

  7. 7

    Returning PromiseValue when creating an object

  8. 8

    Object instantiation using different Constructors

  9. 9

    Why is it returning address instead of object?

  10. 10

    Promise returning a function instead of an object

  11. 11

    Laravel returning "Object" instead of Array?

  12. 12

    Console returning [Object object] instead of object members

  13. 13

    Object returning null after creating object

  14. 14

    Returning a object gets true instead of beeing an object

  15. 15

    Problem with Python returning a NoneType object instead of the Object

  16. 16

    Creating a User object instead of an NSEntityDescription object

  17. 17

    No Visible constructors for class when creating Java object in Jython

  18. 18

    Creating json object in mvc and returning from controller

  19. 19

    Using Delphi object constructors on preallocated memory

  20. 20

    Using an object in a single class with multiple constructors

  21. 21

    Using multiple constructors with one object in Java

  22. 22

    Thymeleaf returning a String array instead of List<Object>

  23. 23

    javascript push returning number instead of object

  24. 24

    jwplayer returning function instead of player object

  25. 25

    Django HttpResponse returning object instead of stated string

  26. 26

    Linq select returning string instead of object

  27. 27

    NGRX 8 reducer returning Object instead of Array

  28. 28

    simple string split is returning object instead array

  29. 29

    Python returning Hexadecimal address instead of object

HotTag

Archive