相关疑难解决方法(0)

JavaScript对象可以有一个原型链,但也可以是一个函数?

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 inheritance

17
推荐指数
3
解决办法
2031
查看次数

Javascript中类似Python的"类"

我想知道如何在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)

谢谢.

javascript python class

4
推荐指数
3
解决办法
1552
查看次数

标签 统计

javascript ×2

class ×1

inheritance ×1

python ×1