JS-访问范围之外的函数参数数组 [英] JS - Accessing Function Parameters Array Outside of Scope

查看:100
本文介绍了JS-访问范围之外的函数参数数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

BRIEF::我经常光顾SO多年了,但这是我的第一篇帖子.我已经为此进行了大量搜索,因此对不起,我很抱歉,这是重复的.

BRIEF: I have frequented SO for years, but this is my first post. I've heavily searched SO for this, so I'm sorry if I overlooked it and this is a duplicate.

function actionFunction(values) {
    this.defaultValues = {
        valueX : 2.5,
        valueY : 5.5
    };
    this.valuesRanges = {
        xRange : { min : 0, max : 10 },
        yRange : { min : 5, max : 10 }
    };
 };

很明显,我可以使用this.defaultValues.valueX等在函数本身中访问它们.我想知道的是如何在函数外部访问它们?我知道这很可能是return或示波器/吊装问题,只是不知道如何进行攻击.

Obviously I can access these within the function itself using this.defaultValues.valueX, etc. What I'd like to know is how to access these outside of the function? I'm aware this is most likely a return or scope/hoisting problem and just do not know how to attack it.

本质上,我需要获取actionFunction.defaultValues.valueX.另外,我想获取actionFunction.valuesRanges.xRange内部的值.我已经尝试将它们作为数组或其他方法使用,但无法找出正确,正确的方法.我有许多需要像这样构造的函数,因此我需要从中提取这些值,所以以模块化的方式做到这一点而又不以jQuery为$.extend()以及其他基础功能的情况为代价,那就太好了.

Essentially, I need to get actionFunction.defaultValues.valueX. Also, I'd like to get the values inside of actionFunction.valuesRanges.xRange. I've tried these as arrays and more, but just can't figure out the correct, proper methodology. I have a multitude of functions structured like this that I need to pull these values from, so a modular way to do so without bringing in jQuery just for $.extend() and other basal functionality at the cost of overhead would be great.

编辑,在发布此信息后,我意识到我忘了提到自己正确地呼叫了var example = new actionFunction();了,但是由于某些原因,我/我遇到了问题.

EDIT I realized after posting this that I forgot to mention I was properly calling var example = new actionFunction();, but for some reason am/was having issues.

也就是说,与this方法相反,用哪种更干净,更逻辑,更可靠的方式将这些值存储在函数中,以便仍然可以像前面提到的那样通过外部调用来抓住它们?

That said, what would be a cleaner and more logical, reliable way to store these values within the function as opposed to the this method so that they could still be grabbed by an exterior call for the values like previously mentioned?

推荐答案

似乎您误解了使用使用this的函数的精确度.通常,我们使用这些类型的函数基于它们来构造对象-我们称这些函数为构造函数".要使用构造函数,必须使用new关键字,并传递所有必要的参数.例如,如果我们有以下构造函数:

It seems you're misunderstanding how exactly you should go about using functions that use this. Generally, we use these sorts of functions to construct objects based on them -- we call these functions "constructors". To use a constructor, you have to use the new keyword, passing any necessary arguments. For example, if we had this constructor:

function Test() { this.a = 1; }

我们可以通过执行以下操作来创建实例(基于构造函数的对象):

We could create an instance (an object based on a constructor) by doing:

var foo = new Test();
console.log(foo.a); // 1

但是,如@Tibos所指出的那样,将其作为常规函数调用将无法按预期方式工作-这可能不是您想要的.在大多数情况下,它将在全局范围内定义(默认情况下,变量将在此范围内定义).但是,您不应像普通函数那样调用构造函数,因为this的值在某些情况下可能会发生变化,并且在函数中误用this可能会误导其他人.

However, calling it as a normal function will not work as intended, as @Tibos pointed out -- that's probably not what you wanted. In most circumstances, it'll be defined in the global scope (that's where variables go by default). However, you shouldn't call a constructor like a normal function, as the value of this can change in some situations and the misuse of this in your function is likely to mislead others.

var foo = Test(); // no "new"
console.log(a); // 1
console.log(foo.a); // TypeError: Cannot read property 'a' of undefined

让我们回到您的原始构造函数.通过将new与构造函数一起使用,而不是将其作为常规函数调用,我们可以这样做:

Let's go back to your original constructor. By using new with your constructor, instead of calling it as a normal function, we can do this:

function actionFunction(values) {
    this.defaultValues = {
        valueX : 2.5,
        valueY : 5.5
    };
    this.valuesRanges = {
        xRange : { min : 0, max : 10 },
        yRange : { min : 5, max : 10 }
    };
}

var example = new ActionFunction();
console.log(example.defaultValues.valueX); // 2.5

如果愿意,可以通过检查确保它始终返回实例:

If you'd like, you can make sure that it always returns an instance by doing a check:

if (!(this instanceof actionFunction)) return new ActionFunction(arg1, arg2, ...);

尽管总是记住对构造函数使用new比将其包括在内要简单得多-大多数人都不会在代码中包括此检查.

It's usually simpler though to always remember to use new for constructors than to include this though -- most people don't bother including this check in their code.

这篇关于JS-访问范围之外的函数参数数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆