I want to be able to use methods that I haven’t required() at the beginning of the file.
Something like this:
var contact = require('contact');
person = contact.create({
'name': createName()
});
Here I want to use the function createName() even if I haven’t required() it explicitly.
Here are examples in Ruby:
# By extending a class it gets the class methods from the parent:
class Section < ActiveRecord::Base
belongs_to :document
has_many :paragraphs
end
# By using a block and executing it in an object containing those methods used
namespace "admin" do
resources :posts, :comments
end
It doesn’t have to be exactly like the example, but somehow inject methods/variables into the code without explicitly using require(), so it would be as elegant and simple as Ruby.
Is this possible in Javascript?
EDIT: It is possible to just use createName() and its not required to export it. But it is required for you to export the module that contains it.
Example: (test2.js)
(test1.js)