gpt4 book ai didi

Javascript,将属性插入父级范围

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

我正在构建一些 node.js 模块,并且我有一些库想要推送到这个对象中

在这个场景中,我有

app.js

var api = require('./scripts/api.js');
var oauth = require('./scripts/oauth.js');
var db = require('./scripts/db.js')
var libraries = {
api : api,
db : db,
oauth : oauth,
}

var modules = require('./scripts/module.js');
modules.init(app, libraries);

模块.js

module.exports = { 

init : function(app,libraries) {
for (key in libraries) {
if (libraries.hasOwnProperty(key)) {
this[key] = libraries[key]
}
}

this.oauth.init(app,libraries);
}
}

api.js

module.exports = { 

init : function(app, libraries) {
for (key in libraries) {
if (libraries.hasOwnProperty(key)) {
this[key] = libraries[key]
}
}
app.get('/api/linkedin/posts', function (req, res) {

//get the credentials
var userid = req.session.user_id;
var credentials = '';
getCredentials('linkedin',userid)
.then(function(result) {
this.db.store(credentials = JSON.parse(result))
})
});
},
}

它工作得很好,但是我想要发生的不是将它推到模块对象本身上。将其插入对象范围,这样我就不必将 this.library.function() 添加到所有内容中。这样我就可以调用oauth.init()并直接访问库,有什么好的方法吗?

我想要实现的效果是与我执行以下操作具有相同的效果,我只是想让这些引导方法变得神奇

模块.js

var api = {}
var oauth = {}

module.exports = {

init : function(app,libraries) {
api = libraries.api
oauth = libraries.oauth

oauth.init(app,libraries);
}
}

这是一个演示问题的 fiddle http://jsbin.com/cadejukijudu/1/edit

最佳答案

@Matthew Bucci:你的问题让我感到非常困惑 - 不是因为你的编码风格,而是因为问题的描述和程序设计 - 所以我首先稍微重写你的问题并添加注释。希望这个问题及其答案会变得显而易见;如果不是我,那就是其他人。

<小时/>

您的问题:

App.js

var libraries = {
"api": require('./scripts/api.js'),
"db": require('./scripts/db.js'),
"oauth": require('./scripts/oauth.js')
};

var modules = require('./scripts/module.js');
modules.init(app, libraries);
// Where does the app variable come from?
// The naming of your script "module.js" is confusing,
// because node has a built-in module called module...

Module.js

module.exports = {
"init": function (app, libraries) {
var key;
for (key in libraries) {
if (libraries.hasOwnProperty(key)) {
this[key] = libraries[key];
// So you effectively want to copy all properties of libraries
// to the object in the variable modules in app.js?
}
}
this.oauth.init(app, libraries);
// I assume the oauth object, required in app.js, also has an init property.
// So you effectively want to copy all properties of libraries to the modules
// object and to the oauth object contained in the modules object!?
// Just pointing out, you're using the same app variable passed in app.js,
// which in this context is an argument of the module.js .init function.
}
};

api.js等等

<小时/>

猜测答案:

init.js

global.api = require('./scripts/api.js');
global.oauth = require('./scripts/oauth.js');
global.db = require('./scripts/db.js');
// Any initialization of required objects is best done when the respective code is
// evaluated, i.e. the first time they're required; Initialization code should be
// included in respective modules.

app.js

api; // Not available yet!
require('./scripts/init.js');
api; // Refers to the result of require('./scripts/api.js') initialized in init.js
oauth; // idem, etc.
<小时/>

结论:我认为您正在处理一个不应该处理的问题。对象没有作用域,也没有父对象。嵌套对象形成的属性树是一条单向街道,因为单个对象可以是多个其他对象的属性;因此,一般来说,从嵌套对象中检索对嵌套对象的引用是非常不可行的;这将需要您编写一个非常复杂的搜索函数,而执行起来会非常昂贵。如果您想将一个对象的属性复制(推送)到另一个对象(无论一个对象是否嵌套在另一个对象中),只需创建一个通用的复制函数即可;您必须明确告诉函数需要将哪些内容复制到哪些内容上。

函数具有作用域,并且使用 let 语句 block 语句也可以具有作用域。对象的大括号不是 block 语句。

关于Javascript,将属性插入父级范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25319658/

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