gpt4 book ai didi

javascript - 'class'实例创建时的回调

转载 作者:行者123 更新时间:2023-11-30 12:57:23 25 4
gpt4 key购买 nike

var foo = (function(){

var c = function(requests) {
bar();
};

c.prototype = {
bar: bar
};

return c;

})();

var f = new foo();

f.baz(function(){
console.log('new instance of foo created');
});

http://jsfiddle.net/LecJM/

我想创建一个回调函数,在创建“类”foo 的新实例时调用该函数。这可能吗?显然上面的代码无法编译,我只是想让您了解我要实现的目标。

最佳答案

var Foo = function (createdCallback) {   
createdCallback();
return this;
};

var bar = new Foo(function () {
console.log("instance created");
});

这就是你想要的吗?

关于javascript - 'class'实例创建时的回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18568984/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com