2015-05-28 11 views

risposta

7

"Scambio prototipo surrogato" (che dubito sia una cosa reale per cominciare) utilizza un oggetto solo per assegnarlo al suo prototipo. Quella variabile viene utilizzata solo una volta:

// Naked function reference for surrogate-prototype-swapping. 
var Ctor = function(){}; 

var nativeCreate = Object.create; 

// An internal function for creating a new object that inherits from another. 
var baseCreate = function(prototype) { 
    if (!_.isObject(prototype)) return {}; 

    if (nativeCreate) return nativeCreate(prototype); 

    Ctor.prototype = prototype; 
    var result = new Ctor; 
    Ctor.prototype = null; 
    return result; 
}; 

E 'usato per fare una versione cross-browser di Object.create. Non è possibile creare direttamente una nuova istanza di un oggetto prototipo, quindi creare un oggetto temporaneo con l'oggetto prototipo come prototipo e restituire una nuova istanza di tale oggetto.

+0

Hai risposto alla mia domanda, ma per il bene di coloro che seguiranno, ho intenzione di rompere un po 'di più. Perfavore, correggimi se sbaglio. – user1294511

+0

Interessante ... ma non vedo come ciò richieda l'uso di un riferimento di funzione globale. Non potresti semplicemente definire 'function() {}' all'interno del factory 'baseCreate'? –

+0

@JoelCornett: La stessa ragione per cui usano 'Ctor.prototype = null'. Potrebbe correre più veloce? – Blender

16

Realizzato dalla risposta Blender. Destinato a quelli del mio livello.

Sebbene non si tratti di un vero e proprio termine, la seguente è una ripartizione del significato previsto di surrogate-prototipo-swapping mediante un commento più completo del codice originale underscore.js.

// A function which will be used as a constructor function in order 
// to add a prototype to a new object. There is nothing special about 
// this function, except how it will be used. 
var Ctor = function(){}; 

// Create a shortcut to Object.create if it exists. Otherwise 
// nativeCreate will be undefined 
var nativeCreate = Object.create; 

// An internal function that will use or act as a polyfill for 
// Object.create, with some type checking built in. 
var baseCreate = function(prototype) { 
    // Check if the object passed to baseCreate is actually an object. 
    // Otherwise simply return an object (from an object literal), 
    // because there is not a valid object to inherit from. 
    if (!_.isObject(prototype)) return {}; 

    // If Object.create is implemented then return the value 
    // returned by Object.create when the prototype parameter is 
    // passed into it. If Object.create has already been 
    // implemented there is no need to recreate it. Just return 
    // its return value. 
    if (nativeCreate) return nativeCreate(prototype); 

    // If Object.create is not defined then Ctor comes into play. 
    // The object passed to baseCreate is assigned to the prototype 
    // of Ctor. This means when Ctor is called prototype will be 
    // the prototype assigned to this (the keyword this). 
    Ctor.prototype = prototype; 
    // Because Ctor is called with the new keyword this (the 
    // keyword this) is returned returned by Ctor. Thus, the 
    // variable 'result' is assigned an object with a prototype 
    // equal to baseCreate's parameter 'prototype'. 
    var result = new Ctor; 
    // Then to reset things Ctor.prototype is set to null. 
    Ctor.prototype = null; 
    // The newly created object, whose prototype is the object 
    // passed to baseCreate is returned. 
    return result; 
}; 
Problemi correlati