node.js - Require usage techniques -


i've seen node.js boilerplates autoload model files using "require()" in several different ways. i'm used using "var variable_name = require('app/models/model.js') , model = require('app/models/model.js') i'm wondering how use model when required this:

require('app/models/model.js') 

let's suppose model.js has attribute "name" , method ".save()".

how model can used?

if don't assign object returned require, can't access later. here examples notes:

this run top-level code, not keep reference module object

require('app/models/model.js') 

this run top-level code , 1 instance (note i'm assuming model.js exports constructor function common opposed name , save properties describe directly, think unlikely what's there).

var mymodel = new require('app/models/model.js') 

this store model constructor can make many instances need.

var model = require('app/models/model.js') 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -