gpt4 book ai didi

javascript 如何创建函数服务(构造函数)

转载 作者:行者123 更新时间:2023-12-02 14:12:44 25 4
gpt4 key购买 nike

在 AngularJs 中,如果调用服务方法:

app.service('nameService', function(){
this.Service = function (){console.log('hello')}
}

然后你就可以使用这个服务(对象):nameService.Service()所以我的问题是如何编写具有两个参数(名称,函数)的函数

function service ( name, function ){
// how angular declare object with that value of first argument ?
}

我问如何编写像 service(name,fn){} 这样的函数?

最佳答案

AngularJs 使用 Dependency Injection注入(inject)服务。

“service”方法可用于 Angular 对象,因为它位于其原型(prototype)上。

angular.prototype.service = ...

您可以阅读有关原型(prototype)继承的更多信息,MDN - Inheritance and the prototype chain

关于javascript 如何创建函数服务(构造函数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39368874/

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