What are the advantages / disadvantages of creating functional objects in JavaScript?

I just watched Douglas Crockford talk about how prototypical inheritance is "also bad"

YouTube 35m55s

I am not interested in his views on prototype inheritance combined with JavaScript, since it is such an important part of the language that it will always be there.

But I would like to know what benefits I get using the creation of the functional object that it shows in the link:

// Class Free Object Oriented Programming
function constructior(init) {
    var that = other_constructor(init),
        member,
        method = function () {
            // init, member, method
        };
    that.method = method;
    return that;
}

After the video, I will re-read the part about creating a functional object in his book "Good JavaScript Details" Chapter 5: Inheritance.

But I can’t see the big difference . I can get private members just fine with the constructor template:

function Constructor (value) {
    var private = value;
    this.getPrivate = function () {
        return private;
    }
}
var OBJ1 = new Constructor(5);
var OBJ2 = new Constructor('bacon');

console.log( OBJ1.getPrivate() ); // 5
console.log( OBJ2.getPrivate() ); // bacon

, , new. new, new.

:

var panda = createBear();

:

var panda = new Bear();

, . , new , . , , . , ?

+4
1

, , , , , .

TL; DR:

. , , , "" , . , , , .

new , , "use strict"; , , new.

/, Factory . , , . , "" .

, , , , , . , , - , . ( , lol jk!)


@Domenic JavaScript , , ? .

new , . , Prototype.

var Constructor = function () {
    this.x = 0;
    this.y = 0;
};
var A = new Constructor();
console.log(A instanceof Constructor ); // true

, , . , , .

:

Constructor.prototype.color = 'yellow';

, Constructor, .color, .

var A = new Constructor();
console.log(A.color); // yellow
console.log(A.hasOwnProperty('color')); // false

JavaScript , , "" , , "" .

var A = new Constructor();
Constructor.prototype.food = 'bacon';
console.log(A.food); // bacon;

, , , .

Constructor.prototype.number = 5;
A.calculate = function () {
    return A.number * 5;
}
console.log(A.calculate()); // 25

Constructor.prototype.number = 'fishsticks';
console.log(A.calculate()); // NaN

, , V8, " ". , . JavaScript V8

. .__proto__. Object.getPrototypeOf().

console.log(Object.getPrototypeOf(A.color)); // yellow

, Crockford , , new. new , .

var A = Constructor();
console.log(A); // undefined

, , new.

var Constructor = function () {
    "use strict";
    this.x = 0;
    this.y = 0;
}
var A = Constructor();

console.log(A);
// Uncaught TypeError: Cannot set property 'x' of undefined

Factory

. new "" "", , .

function factory () {
    var obj = {
        x: 0,
        y: 0
    }
    return obj;
}
var A = factory(); // {x: 0, y: 0}

, - , .

function factory () {
    if ( new Date().getHours() < 8 ) { 
        return "can't create object. Need Coffe!" 
    };
    var obj = {
        x: 0,
        y: 0
    }
    return obj;
}
var A = factory(); // Before 8 am: "can't create object. Need Coffe!"
var A = factory(); // After 8 am: {x: 0, y: 0};

, / . .

factory.prototype.foo = "bar";
A = factory();
console.log(A.foo); // undefined

, . , .

.

. TL; DR

, , - -.

:

JavaScript , , ?

vs Factory

JavaScript

+4

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


All Articles