最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How use prototype in node.js - Stack Overflow

programmeradmin0浏览0评论

I write my first module on nodejs. I need parse my site from google cache. Post is map of table post. When i try use this module i have this error: "TypeError: Cannot set property 'prototype' of undefined" How fix this error?It's my code:

module.exports = function Post(documentDOM,options)
{
    this.opts = $.extend({id:0,author_id:0},options);
    this.doc = documentDOM;
    this.post  = {
        id: 0,
        name: '',
        alt_name: '',
        notice: '',
        content: '',
        author: '',
        author_id: 0,
    };
}

module.exports.Post.prototype = {
    init: function() {
        this.post.id = this.opts.id;
        this.post.author_id = this.opts.author_id;
    },

    content: function() {
        content = this.doc.find('.fullnews-content').html();
        if(!content.length)
            content = doc.find('.article-content').html();
        return content;
    }
}

Thank.

I write my first module on nodejs. I need parse my site from google cache. Post is map of table post. When i try use this module i have this error: "TypeError: Cannot set property 'prototype' of undefined" How fix this error?It's my code:

module.exports = function Post(documentDOM,options)
{
    this.opts = $.extend({id:0,author_id:0},options);
    this.doc = documentDOM;
    this.post  = {
        id: 0,
        name: '',
        alt_name: '',
        notice: '',
        content: '',
        author: '',
        author_id: 0,
    };
}

module.exports.Post.prototype = {
    init: function() {
        this.post.id = this.opts.id;
        this.post.author_id = this.opts.author_id;
    },

    content: function() {
        content = this.doc.find('.fullnews-content').html();
        if(!content.length)
            content = doc.find('.article-content').html();
        return content;
    }
}

Thank.

Share Improve this question asked Jan 20, 2012 at 17:46 v.tsurkav.tsurka 4526 silver badges18 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6
module.exports = function Post((documentDOM,options)

I think you meant

module.exports.Post = function((documentDOM,options)

And then access it like this

var Post = require('./post.js').Post;

With the first you're making exports itself a named function, to modify it you would use module.exports.prototype.

Relevant study material: http://kangax.github./nfe/

发布评论

评论列表(0)

  1. 暂无评论