如何从对象输出中删除函数?

我正在学习构造函数模式

为了锻炼我正在学习的内容,我正在构build一个名为Book受到Mongoose API启发的内存模型:

 var assert = require("assert"); var Book = (function() { var books = []; var constructor = function(title, author, genre) { assert.ok(title, "title cannot be undefined"); assert.ok(author, "author cannot be undefined"); assert.ok(genre, "genre cannot be undefined"); this.title = title; this.author = author; this.genre = genre; this.save = function() { books.push(this); }; this.description = this.title + "is a " + this.genre + " by " + this.author; }; constructor.find = function() { return books; }; return constructor; }()); 

有了这个模型,我可以创buildBook实例并将其save到内存中:

 var book = new Book("The Great Gatsby", "F. Scott Fitzgerald", "Novel"); book.save(); var books = Book.find(); console.log(books); // [ { title: 'The Great Gatsby', // author: 'F. Scott Fitzgerald', // genre: 'Novel', // save: [Function], // description: 'The Great Gatsbyis a Novel by F. Scott Fitzgerald' } ] 

如何从输出中删除函数属性“保存”? 我只想显示属性。

我需要知道,因为我想用Express book发送给客户端,我不想用function卷积响应。

(我来自C#背景,在C#中,我会覆盖System.Object基类中的一个名为ToString函数,它的function类似于 console.log我不知道JavaScript中有什么等价物。

是的,可以覆盖默认的toString输出:

 var Book = (function() { var books = []; var constructor = function(title, author, genre) { assert.ok(title, "title cannot be undefined"); assert.ok(author, "author cannot be undefined"); assert.ok(genre, "genre cannot be undefined"); this.title = title; this.author = author; this.genre = genre; this.save = function() { books.push(this); }; this.description = this.title + "is a " + this.genre + " by " + this.author; }; constructor.find = function() { return books; }; constructor.prototype.toString = function() { return JSON.stringify(this); }; return constructor; }()); 

在原型上定义函数:

 var Book = (function() { var books = []; function Book (title, author, genre) { assert.ok(title, "title cannot be undefined"); assert.ok(author, "author cannot be undefined"); assert.ok(genre, "genre cannot be undefined"); this.title = title; this.author = author; this.genre = genre; this.description = this.title + "is a " + this.genre + " by " + this.author; } Book.prototype.save = function () { books.push(this); }; Book.find = function () { return books; }; return Book; }()); 

如果我可以帮助它,我想封装在书的构造函数的行为。

这听起来像一个奇怪的要求,但如果你真的需要(无论什么原因),你可以使用Object.defineProperty来定义函数,而不使其可枚举:

 var Book = (function() { var books = []; function Book (title, author, genre) { assert.ok(title, "title cannot be undefined"); assert.ok(author, "author cannot be undefined"); assert.ok(genre, "genre cannot be undefined"); this.title = title; this.author = author; this.genre = genre; this.description = this.title + "is a " + this.genre + " by " + this.author; Object.defineProperty(this, 'save', { value: function () { books.push(this); } }); } Book.find = function () { return books; }; return Book; }()); 

简短但很冒险的方式:

 books = JSON.parse(JSON.stringify(books)); console.log(books); 

注意:当通过使用res.json(book)快递将对象传递给客户端时, JSON.stringify发生。 如果你只想把对象发送给客户端,那么你不需要做任何事情,只要把你的对象传递给res.json([obj|array])

封装对象本身的行为。

 var Book = (function() { var books = []; var constructor = function(title, author, genre) { assert.ok(title, "title cannot be undefined"); assert.ok(author, "author cannot be undefined"); assert.ok(genre, "genre cannot be undefined"); this.title = title; this.author = author; this.genre = genre; this.save = function() { books.push(this); }; this.description = this.title + "is a " + this.genre + " by " + this.author; this.stripFunctions = function () { var item = this; Object.keys(item).forEach(function (key) { if (item.hasOwnProperty(key) && typeof item[key] === 'function') { delete item[key]; } }); return item; }; }; constructor.find = function() { return books; }; return constructor; }()); books = books.map(function (book) { return book.stripFunctions(); }); console.log(books); 

请注意,上述方法将从您的对象实例中删除任何函数,这意味着之后将不能再调用它们。

PS – 这不是你的问题的一部分,但你应该考虑添加方法到你的构造函数的原型,这样你就不会在每次创buildBook的实例时创build新的函数。

此外,我会重新迭代,如果你使用res.json从express中发送对象,那么它JSON.stringify为你调用JSON.stringify ,并且当你对一个对象进行stringify的时候,函数会被剥离掉。