Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/89.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 基于confirm()结果创建新对象_Javascript_Html - Fatal编程技术网

Javascript 基于confirm()结果创建新对象

Javascript 基于confirm()结果创建新对象,javascript,html,Javascript,Html,有人能帮我理解为什么这样不行吗。以下是我的JS代码: function Agent(codename, str, acc, spd, int, lock, cqc, stl, lck) { "use strict"; this.codename = codename; this.str = str; this.acc = acc; this.spd = spd; this.int = int; this.lock = lock; t

有人能帮我理解为什么这样不行吗。以下是我的JS代码:

function Agent(codename, str, acc, spd, int, lock, cqc, stl, lck) {
    "use strict";
    this.codename = codename;
    this.str = str;
    this.acc = acc;
    this.spd = spd;
    this.int = int;
    this.lock = lock;
    this.cqc = cqc;
    this.stl = stl;
    this.lck = lck;
    this.avg = function () {
        return (this.str + this.acc + this.spd + this.int + this.lock + this.cqc + this.stl + this.lck) / 8;
    };
}

document.getElementById("ace-select").addEventListener("click", function () {
    "use strict";
    var r = confirm("Are you sure you want Ace?");
    if (r === true) {
        var ace = new Agent("Ace", 8, 9, 9, 9, 8, 10, 10, 5);
    }
}, false);
以下是我的按钮HTML:

<button class="btn btn-primary btn-lg btn-border select-color" id="ace-select" type='submit' role="button">Select</button>
与前面一样,会弹出确认框,当我单击确定时,它会按预期计算为true,并创建我的对象


我做错了什么,当我把它放在事件侦听器中时它就不能工作了?

问题在于变量范围。由于ace被声明为匿名函数中的一个变量,因此它只能由该函数访问

这就是为什么当您试图通过控制台或其他任何地方(例如该函数之外的另一个函数)访问它时会出现错误

这就是JavaScript的工作原理,这里有关于作用域的更多信息:

如果您想在ace的本地范围之外使用ace,那么如果您使用不同的设计模式,有几种方法可以实现。例如,一个启示模块:这本书有很多关于JavaScript设计模式的精彩而免费的信息,我强烈推荐阅读

另一种方法是简单地尝试创建自定义对象:

AGX = {};
AGX.Learning = {
    createAgent: function(options) {
        console.log(AGX.Learning.agent);
        console.log((options.str + options.acc + options.spd + options.int + options.lock + options.cqc + options.stl + options.lck) / 8);
    }
}

var createAgentOptions = {
    str: 2,
    acc: 5,
    spd: 7,
    int: 2,
    lock: 30,
    cqc: 4,
    stl: 3,
    lck: 29
};
AGX.Learning.agent = 'Ace';
AGX.Learning.createAgent(createAgentOptions);
在上面的示例中,Learning是一个添加到父对象AGX中的对象。从那里,代理也存储在学习对象中,然后由createAgent函数使用


我建议您尝试一下,尽管这是一个基本的工具,但它可以帮助您学习JS基础知识。JS中的几乎所有内容都是一个对象,然后在链接的书中介绍不同的设计模式。

如果我要以完全不同的方式来做,请随时提出建议。我这样做的目的是为了获得编写代码的经验,我完全希望找到需要解决的问题,比如这样的问题,我很清楚学习最佳实践将是其中的一部分。我认为错误不在声明中。您是否在那里调用ace以获取输出?ace上的作用域仅在if中,而不在其他任何地方。ace的作用域是它所在的函数,而不是if语句。您可以通过在if语句之后添加console.logace来测试这一点。你完全正确。我以后会记住这一点。非常感谢!很乐意帮忙:。
AGX = {};
AGX.Learning = {
    createAgent: function(options) {
        console.log(AGX.Learning.agent);
        console.log((options.str + options.acc + options.spd + options.int + options.lock + options.cqc + options.stl + options.lck) / 8);
    }
}

var createAgentOptions = {
    str: 2,
    acc: 5,
    spd: 7,
    int: 2,
    lock: 30,
    cqc: 4,
    stl: 3,
    lck: 29
};
AGX.Learning.agent = 'Ace';
AGX.Learning.createAgent(createAgentOptions);