function a () {
return "foo";
}
a.b = function () {
return "bar";
}
function c () { };
c.prototype = a;
var d = new c();
d.b(); // returns "bar"
d(); // throws exception, d is not a function
Run Code Online (Sandbox Code Playgroud)
有没有办法d成为一个函数,但仍然继承属性a?
我想知道如何在Javascript中制作类似于Python中的"类".获取此处列出的Python类和函数:
class one:
def foo(bar):
# some code
Run Code Online (Sandbox Code Playgroud)
函数"foo"将被调用one.foo(bar).
JS等价物是什么?我怀疑它会是这样的:
var one = {
foo: function(bar) {
// JavaScript
}
};
Run Code Online (Sandbox Code Playgroud)
谢谢.