Javascript 混合数据类型、函数和对象
当您使用Javascript 混合数据类型、函数和对象,javascript,node.js,Javascript,Node.js,当您使用require并加载模块时,您似乎能够创建具有属性的函数或可以作为函数调用的对象 console.log(require('events').EventEmitter); //{ [Function: EventEmitter] defaultMaxListeners: 10, listenerCount: [Function] } 我以前使用require模块做过这件事,所以我可以复制它。但我想手动创建更多。在这方面,我有一些问题: 是否可以手动复制此数据,在不需要的情况下进行h
require
并加载模块时,您似乎能够创建具有属性的函数或可以作为函数调用的对象
console.log(require('events').EventEmitter);
//{ [Function: EventEmitter] defaultMaxListeners: 10, listenerCount: [Function] }
我以前使用require
模块做过这件事,所以我可以复制它。但我想手动创建更多。在这方面,我有一些问题:
的情况下进行harcoded
defaultMaxListeners
不是来自prototype。代码:
EventEmitter.defaultMaxListeners // 10
EventEmitter.prototype.defaultMaxListeners // undefined
EventEmitter() //No error
function SomeConstructor(){
this.instanceProperty = 'foo';
}
// Instance Method
SomeConstructor.prototype.method = function(){
console.log('method');
}
// Static Method
SomeConstructor.staticMethod = function(){
console.log('static');
}
var test = new SomeConstructor();
test.method();
SomeConstructor.staticMethod();
谢谢我从未读过这方面的书。这是一个有点模糊的Javascript部分。@b在现实世界中,一个很好的例子是jQuery,在jQuery中,您可以像调用函数一样调用
$
(选择器),但也可以像使用命名空间一样使用它,如$.ajax()
。