2017-08-28 2 views
0

Comment exporter un objet sans ses fonctions?ES6 comment exporter un objet sans ses fonctions

Un modèle utilisateur:

export default { 
    data: {}, 

    sanitize (options) { 
    }, 

    async insert (options) { 
    }, 

    async find (options) { 
    }, 

    async remove (options) { 
    } 
} 
utilisation

:

const result = await user.insert({ id: '123', name: 'haha xxxx', password: 'gskgsgjs' }) 
console.log(user) 

Résultat:

{ data: { id: '123', name: 'haha', _id: 59a40e73f63b17036e5ce5c4 }, 
    sanitize: [Function: sanitize], 
    insert: [Function: insert], 
    find: [Function: find], 
    remove: [Function: remove] } 

Ce que je suis après:

{ data: { id: '123', name: 'haha', _id: 59a40e73f63b17036e5ce5c4 } 

Des idées?

EDIT:

En utilisant la classe ES6:

export default class User { 
    constructor(options) { 
    this.data = this.sanitize(options) 
    } 

    sanitize (options) { 
    } 

    async insert (options) { 
    } 

    async find (options) { 
    } 

    async remove (options) { 
    } 
} 

Utilisation:

let User = new user() 
    // Inject a doc. 
    const result = await User.insert({ id: '123', name: 'haha xxxx', password: 'gskgsgjs' }) 
    console.log(User) 

Résultat:

User { 
    data: { id: '123', name: 'haha xxxx', _id: 59a4143e63f3450e2e0c4fe4 } } 

encore, non exactement ce que je suis après:

{ data: { id: '123', name: 'haha', _id: 59a40e73f63b17036e5ce5c4 } 
+0

Qu'est-ce que 'insert'? Pourquoi exactement vous avez besoin de l'exporter quand vous l'avez déjà en tant que propriété 'user'? – estus

+0

@estus désolé le résultat est réellement correct. j'ai mal compris. – laukok

+0

Comment l'utilisateur est-il supposé appeler 'user.insert' si cette méthode n'est pas disponible sur l'objet? –

Répondre

1

Vous pouvez utiliser les classes ES6 plutôt que d'utiliser un objet. Vous pouvez trouver un exemple here.

// A base class is defined using the new reserved 'class' keyword 
class Polygon { 
    // ..and an (optional) custom class constructor. If one is 
    // not supplied, a default constructor is used instead: 
    // constructor() { } 
    constructor(height, width) { 
    this.name = 'Polygon'; 
    this.height = height; 
    this.width = width; 
    } 

    // Simple class instance methods using short-hand method 
    // declaration 
    sayName() { 
    ChromeSamples.log('Hi, I am a ', this.name + '.'); 
    } 

    sayHistory() { 
    ChromeSamples.log('"Polygon" is derived from the Greek polus (many) ' + 
     'and gonia (angle).'); 
    } 

    // Method to get json string 
    toJson() { 
    return JSON.stringify({ name: this.name, height: this.height, weight: this.weight }); 
    } 

    // We will look at static and subclassed methods shortly 
} 
+0

Vous pouvez ajouter une fonction 'toJson()' à la classe et renvoyer un objet JSON stringifié ou vous pouvez simplement retourner un nouvel objet comme vous voulez l'imprimer comme 'console.log (result.toJson());'. Je suis en train d'éditer ma réponse. – bennygenel