假设我有一个对象A:
#a good old global
logview = "/usr/bin/logview"
#a function that starts logview:
port = 100
log_file = "/foo/bar"
logview_process = subprocess.Popen([logview, log_file, port],
stdout = subprocess.DEVNULL,
stderr = subprocess.STDOUT)
#a separate function that monitors in the background:
while True:
time.sleep(1)
logview_status = 0
try:
logview_status = psutil.Process(logview_process.pid).status()
except psutil.NoSuchProcess:
pass
if(logview_status == psutil.STATUS_STOPPED or
logview_status == psutil.STATUS_ZOMBIE or
logview_status == psutil.STATUS_DEAD or
logview_status == 0):
print("Logview died; restarting")
logview_cli_list = [logview]
logview_cli_list.extend(logview_process.args)
logview_process = subprocess.Popen(logview_cli_list,
stdout = subprocess.DEVNULL,
stderr = subprocess.STDOUT)
if(some_other_condition): break
然后,假设我也是一个对象B:
var A = {
'parameter': "Dura lex sed lex.",
'function_a': function (new_type) {
console.log ("It's working!");
}
};
我需要的是一种在对象B中动态创建方法var B = {
'parameter': "Veni vidi vici!"
};
而无需复制/克隆对象A中的对象A(“Dura lex sed lex。”)的参数的简单方法。保留对象B的参数(“Vini vidi vici!”)。
我该怎么做?
答案 0 :(得分:1)
试一试:
B['function_b'] = A['function_a'];
答案 1 :(得分:1)
你的意思是这样的吗?
var A = {
'parameter': "Dura lex sed lex.",
'function_a': function (new_type) {
console.log ("It's working!");
}
};
var B = {
'parameter': "Vini vidi vici!"
};
var clone = function(origin, target, prefix) {
Object.keys(origin).forEach(function(key) {
if (!target.hasOwnProperty(key)) {
if (key.indexOf("function_") > -1) {
target["function_" + prefix] = origin[key];
}
}
});
}
clone(A, B, "b");
console.log(B);
B.function_b();
答案 2 :(得分:0)
我不知道我是否理解你的问题,但我认为你想要这样的事情:
B.function_b = function(whatever) {
console.log('it works!');
};