Is there any difference in using a constructor to create an object or return an object?

Is there a difference in the functioning of these functions? The first of them, as a rule, refers to what I think about when thinking of a constructor.

Example 1: using this name and set of properties. Then use the new one to create a new Book object.

    function Book(name, numPages) {
        this.name = name;
        this.numPages = numPages;
    }

    var myBook = new Book('A Good Book', '500 pages');

Example 2: returning an object using a new one and just calling the function itself.

    function Movie(name, numMinutes) {
        return { name:name, numMinutes:numMinutes };
    }

    var best = new Movie('Forrest Gump', '150');

    var other = Movie('Gladiator', '180');

I suppose I'm trying to figure out if they are different in how they create the object? If this one is better than the other? Are there different situations when it is better to work on another?

+4
source share
3 answers

- , prototype, instanceof, . : new -keyword, ( constuctor)

apply() , ; , , /.

- factory, . , new -keyword . , , ( JS- , , , ,... )
, , - ()

, , , (, pro con.)

My goto-approach , :
( , factory ).

function Book(conf) {
    var book = Object.create(Book.prototype);
    //a book like this usually has multiple configs/properties
    if(typeof conf === "object"){
        for(var k in conf) book[k] = conf[k];
    }else if(conf){
        //assuming that I get at least the name passed
        book.name = String(conf);
    }
    return book;
}

//I have a prototype that can be extended
//with default-values for example; no idea for a good method 
//to add to the prototype in this example ;)
Book.prototype.numPages = 0;

//but I can also use it like a plain function; no error if you 
var myBook1 = Book("Peter Pan");
var myBook2 = Book({
    name: "American Gods",
    author: "Neil Gaiman"
});

, - Book

function Book(conf) {
    //with this simple line I can also use this as a function to cast anything into a "Book"
    if(conf instanceof Book) return conf;

    var book = Object.create(Book.prototype);
    //...
    return book;
}

var data = [
    "Peter Pan",
    {name: "American Gods"},
    //...
];

var books = data.map(Book);

-, .

+2

, new, JS this. - , . , instanceof - .

function MovieA(title) {
    this.title = title;
}
MovieA.prototype.getTitle = function() {
    return this.title;
};

function MovieB(title) {
    return {
    title: title
  };
}
MovieB.prototype.getTitle = function() {
    return this.title;
};

var a = new MovieA('A');
console.log(a instanceof MovieA); // true
console.log(a.getTitle()); // 'A'

var b = MovieB('B');
console.log(b instanceof MovieB); // false
console.log(b.getTitle()); // Uncaught TypeError: b.getTitle is not a function

, new, , .

, , , , . , factory, , factory .

var Factory = {
  makeThing: function() {
    return { name: 'thing' };
  }
};

// Want to test the case for if makeThing fails inside of some other code
var MockFactory = {
  makeThing: function() {
    return null;
  };
};

, , . new. new. , - , , this , .. , .

0

, .

new Book('A Good Book', '500 pages');

Book , Book.prototype, constructor Book. Book.prototype Object.prototype.

var other = Movie('Gladiator', '180');

Movie factory (new ) , , Object.prototype, constructor Object.

, Object.

0

All Articles