Transition from "prototype" and "new" to a closing and exposure template

I rephrased another user's JavaScript code.

BEFORE:

function SomeObj(flag) {
    var _private = true;
    this.flag = (flag) ? true : false;
    this.version="1.1 (prototype)";
    if (!this._someProperty) this._init();
            // leading underscore hints at what should be a 'private' to me
    this.reset(); // assumes reset has been added...
}

SomeObj.prototype.reset = function() {
    /* perform some actions */
}

/* UPDATE */
SomeObj.prototype.getPrivate = function() {
    return _private; // will return undefined
}

/* ...several other functions appended via `prototype`...*/

AFTER:

var SomeObj = function (flag) {
    var _private = true;
    this.flag = (flag) ? true : false;
    this.version = "2.0 (constructor)";

    this.reset = function () {
       /* perform some actions */
    };

    /* UPDATE */
    this.getPrivate = function() {
        return _private; // will return true
    }

    /* other functions and function calls here */
}

For me, the first example seems difficult to read, especially in a wider context. Adding methods such resetas this using a property prototypeseems to be much less controllable since it could supposedly happen anywhere in the script. My reorganized code (second example above) looks a lot neater for me, and therefore it is easier to read because it is self-sufficient. I got some privacy with variable declarations, but I lost prototype capabilities.

...

QUESTIONS:

  • -, , , prototype, . 6 , , prototype , .

  • new; "" --. , var, , , , , , ( ) , . - :

    var SomeObj = (function () {
    
        /* all the stuff mentioned above, declared as 'private' `var`s */
    
        /* UPDATE */
        var getPrivate = function () {
            return private;
        }
    
        var expose = function (flag) {
             // just returns `flag` for now
             // but could expose other properties
             return {
                 flag: flag || false, // flag from argument, or default value
                 getPrivate: getPrivate
             } 
        };
    
        return {
            expose: expose
        }
    })(); // IIFE
    
    // instead of having to write `var whatever = new SomeObj(true);` use...
    var whatever = SomeObj.expose();
    

    StackOverflow, " " ( , ). , prototype, , new (, instanceof). , - new?

  • , , , : prototype new , ( , ), , - ?

...

UPDATE:

, expose , . , , SomeObj, ( ). flag ( ) expose, . , , ( ?).

, 2: , - new?

, .

+2
4

, , , .prototype - - , , .

"" , .

:

  • . - - , . .

  • " " OO, . new, , - .

  • - " " - , .

+1

- prototype?

, - , , , . prototype:

  • prototype

, . , (, ). , prototype , - , , .

, ( ):

SomeObj.prototype = {
    method1: function () {},
    method2: function () {}
}

, , . , , . , , - .

//Your code
var SomeObj = function (flag) { //...

//Sneaky person code
delete SomeObj.reset;
SomeObj.prototype.reset = function () { /* what now? */ }

new

{}, new. new (function). - , - , .

. , - , , , . , - require.js, "", require, define, , , .

prototype

:

var attachTo = {};
;(function (attachTo, window, document, undefined) {
    Plugin = function () { /* constructor */ };
    Plugin.prototype = { /* prototype methods */ };

    attachTo.plugin = Plugin;
})(attachTo, window, document);
var plugin = new (attachTo.plugin);

http://jsfiddle.net/ExplosionPIlls/HPjV7/1/

+1

:

  • : . ( , ). :

    • /, . JavaScript, , .

    • , . , .

  • : " ", .

    , : .

    , , ​​JavaScript, , . :

    var SomeObj = (function (flag) {
    
        /* all the stuff mentioned above, declared as 'private' `var`s */
    
        var MyObj = function() {}
        MyObj.prototype = {
            flag: flag,
            reset: reset
        };
    
        return {
           expose: function() { return new MyObj(); }
        }
    })();
    

    , requirejs, AMD ( ), AMD, . : JavaScript .

  • , : composejs, dejavu, barman ( - , , ).

    . factory, new .

+1

:

  • , reset prototype , . , , ( , ). , ; function SomeObj var SomeObj = function , SomeObj . , " ", - ...

  • IIFE , instance instanceof SomeObj.

  • Not sure if this answers your question, but there is Object.createone where you can still install the prototype, but get rid of the keyword new. However, you lose the ability to have constructors.

0
source

All Articles