Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 未访问局部变量&';这';上下文强制_Javascript - Fatal编程技术网

Javascript 未访问局部变量&';这';上下文强制

Javascript 未访问局部变量&';这';上下文强制,javascript,Javascript,我有下面一段代码,它给我的工作带来了麻烦[代码后面的问题] var LIB = function(){}; (function(){ var JSExpert = function(lname){ if(!(this instanceof arguments.callee)){ return new JSExpert(lname); } this.fname = "Later "; this.name = this.fname + lname;

我有下面一段代码,它给我的工作带来了麻烦[代码后面的问题]

var LIB = function(){};

(function(){
var JSExpert = function(lname){
    if(!(this instanceof arguments.callee)){
        return new JSExpert(lname);
    }

    this.fname = "Later ";
    this.name = this.fname + lname;
};

JSExpert.prototype = {
    setFname: function(fname){
        this.fname = fname;
        return this; //for chaining
    },

    getName: function(){
        alert("javascript expert name is " + this.name);
    }
};

LIB.Expert = JSExpert;
return;

    window.JSExpert = JSExpert;
})();
现在测试代码来了

LIB.Expert("Edwads").setFname("Dean").getName();
现在,当您尝试代码时,setFname()方法(应该在构造函数中“稍后”由“Dean”更改)没有这样做,为什么会这样

第二个问题:我们在JSExpert内部使用“this”。如何确保“this”的上下文稍后不会绑定到“LIB”的上下文中,并保持强制执行到“JSExpert”的上下文中,从而执行“LIB.Expert”

谢谢,因为:

return;

window.JSExpert = JSExpert;
不正确,应为:

window.JSExpert = JSExpert;
getName: function(){
    alert("javascript expert name is " + this.fname + this.lname);
}

应该是:

window.JSExpert = JSExpert;
getName: function(){
    alert("javascript expert name is " + this.fname + this.lname);
}
lname
需要是类似fname的属性


1) 改变

2) 在contructor函数中,选中此类型:

var JSExpert = function(lname){
    //If not an instance of this class creates a new object of this class and return it
    if(!(this instanceof arguments.callee)){
        return new JSExpert(lname);
    }

    this.fname = "Later ";
    this.name = this.fname + lname;
};

在其他函数中,您可以执行以下操作:
if(!(JSExpert的这个实例))抛出新错误(“”),抛出错误是您在这里能做的最好的事情,这是使用原型时的一个缩进。

我没有在窗口对象上使用JSExpert对象,因此它不会影响cod行为。是的,两个局部变量都可以工作,但是为什么没有更新'this.name'?我不想有一堆变量,当它们都是相关的,并且可以在构造函数中组合成一个变量时。@Eric我不认为你理解这里发生了什么。
this.name
变量设置为
this.fname+lname
的值。在定义时,检索其他变量的值,执行字符串concat,并存储该值。不确定为什么您认为更改
this.fname
会以某种方式影响
this.name
的字符串值我已经做了实例检查。我希望有更好的方法来强制上下文,而不是在50个方法中抛出错误:)感谢setFname()。
setFname: function(fname){
        this.name = this.name.slice(this.fname.length);
        this.name = fname + ' ' + this.name;
    this.fname = fname + ' ';
    return this; //for chaining
},
var JSExpert = function(lname){
    //If not an instance of this class creates a new object of this class and return it
    if(!(this instanceof arguments.callee)){
        return new JSExpert(lname);
    }

    this.fname = "Later ";
    this.name = this.fname + lname;
};