How to structure object in javascript -
how can structure object allow initialization same way stripe api:
var stripe = require("stripe")( "sk_test_bqokikjovbii2hlwgh4olfq2" );
i have tried this
var example = (function () { function example(api_token) { this.token = api_token; } example.prototype.getself = function (callback) { //do stuff }; return example; }()); module.exports = example;
but cannot set property 'token' of undefined
error when calling var sdk = require('./example')(api_key);
since function isn't being called constructor (via new
keyword), need make sure function provide doesn't expect called way.
you this:
function example(api_token) { this.token = api_token; } // ...prototype, etc. function example(api_token) { return new example(api_token); } module.exports = example;
or don't use constructor function @ all, , use object.create
:
var exampleproto = { getself: function() { // ... } }; function example(api_token) { var o = object.create(exampleproto); o.token = api_token; return o; } module.exports = example;
Comments
Post a Comment