What is the difference between using a function to initialize a JavaScript object with and without a return statement?

I have two functions for initializing objects. init and differentInit.

function init(){
    return {a:5};
}

function differentInit(){
    this.a =5;
}

obj = init();
newobj = new differentInit()

how do obj and newobj objects differ?
This is what the JavaScript interpreter shows as the contents of two objects.

>obj
<Β·Object {a: 5}
>newobj
<Β·differentInit {a: 5}

EDIT: as the answers (which are really good) indicated that it newobjhas an additional prototype like differentInit.prototype. I thought that an object can only be created using an additional prototype using the method Object.create(). I don't know if this is too general a question, but how many other ways to create prototype objects? And can the method used in this matter be considered a good way to create an object with prototypes?

+4
2

obj newobj ?

, , , ( constructor) Object.prototype. Object.prototype:

, obj:

                                  +----------+
Object.prototype-------------+--->| toString |
                             |    | valueOf  |
                             |    | ...      |
                             |    +----------+
        +---------------+    |
obj---->| [[Prototype]] |----+
        | a: 5          |
        +---------------+

. newObj:

                                                           +----------+
Object.prototype--------------------------------------+--->| toString |
                                                      |    | valueOf  |
                                                      |    | ...      |
                                                      |    +----------+
                                  +---------------+   |
differentInit.prototype------+--->| [[Prototype]] |---+
                             |    +---------------+
                             |
                             |
        +---------------+    |
newObj->| [[Prototype]] |----+
        | a: 5          |
        +---------------+

( , Oriol , , Object.prototype differentInit.prototype , constructor, Object differentInit . , obj.constructor Object, newObj.constructor differentInit. , ...)

- differentInit.prototype, .


json, , JSON . JSON - . , JSON.


, Object.create().

JavaScript , Object.create(null) ( null [[Prototype]], , ).

... ?

:

  • , . obj = {}, Object.prototype .

  • new ; , prototype, , new. , function ( class), prototype ; . ( prototype null, new Object.prototype.)

    , , new, , function:

    // Using `function`
    function Foo() {
    }
    Foo.prototype.bar = function() {
        console.log("Foo instance says 'bar'!");
    };
    var f = new Foo();
    f.bar();
    

    ES2015, , class:

    class Foo {
        bar() {
            console.log("Foo instance says 'bar'!");
        }
    }
    var f = new Foo();
    f.bar();
    

    , : Foo, new, Foo.prototype bar .

  • Object.create(p), p . # 2 , p null , . ES5, ( null). ( , .)

:

, , ?

"" new differentInit, , . , ( "" , , ). Object.create ( new). , , , Object.create - .

+8

obj.constructor === Object
newobj.constructor === differentInit
Object.getPrototypeOf(obj) === Object.prototype;
Object.getPrototypeOf(newobj) === differentInit.prototype;
obj instanceof differentInit === false
newobj instanceof differentInit === true

( differentInit.prototype), .

+4

Source: https://habr.com/ru/post/1627436/


All Articles