2017-03-02 57 views
-2

改变函数值我有功能的具有财产与this.name和另一个函数b具有相同的属性我想重写的this.name的A到B的this.name如何通过另一个函数在JavaScript

function a(){ 
    this.name = 'john'; 
} 
function b(){ 
    this.name = 'Smith'; 
} 

我想b.name应该是'john'怎么样?

+0

让'用于指定B'功能使用参数'name' – piotrbienias

+0

还有,你想实现的呢? – Rajesh

+0

*“我有一个函数a有this.name的属性”*否,'a'中的'this.name'和函数'a'的'name'属性没有关系。 (在最新的JavaScript引擎上,'a.name'是''''''而'b.name'是''b“')。另外:你必须给我们一大堆* *更多的上下文。你打电话给他们怎么样?为什么你需要追溯地改变一个函数的工作方式? (这通常是 - 但并非总是 - 一个巨大的红色危险信号。) –

回答

-1

也许你应该考虑使用类和对象。此外,函数的'name'属性保留为其实际名称。

function a(){ 
    this.te = 'john'; 
} 
function b(){ 
    a.te = 'Smith'; 
} 

a(); 
b(); 
console.log(a.te); 
0

你可以使用的nested function的概念javascript.Since嵌套函数是一个封闭,这意味着一个嵌套函数可以“继承”它包含函数的参数和变量。换句话说,内部函数包含外部函数的范围。

function a(){ 
 
    this.name = 'john'; 
 
function b(){ 
 
    this.name = 'Smith'; 
 
    return name; 
 
} 
 
return b(); 
 
} 
 
name = a();//call to function a overides the name 
 
console.log(name);

+0

我不认为这是OP所期待的 – Rajesh

相关问题