简体   繁体   English

您如何正确使用Java进行OOP并明确指定变量范围?

[英]How do you properly OOP with Javascript and clearly specify variable scopes?

In Introduction to Object-Oriented JavaScript , a formal description is given on how to use javascript in an object oriented fashion. “面向对象的JavaScript简介”中 ,给出了有关如何以面向对象的方式使用javascript的正式描述。 Your code for functions inside objects would like along the lines of: 您对象内部函数的代码将遵循以下方式:

$(function() {
  var oPerson = new cPerson("Florian");
  alert("Hi! My name is " + oPerson.sMyNameIs());
});

function cPerson(sSetName)
{
  var sName= sSetName;
  var oPersonInst = this;

  this.sMyNameIs = function () {
    return sName;
  };
}

With a bit more experience, you probably want a more clearcut reference to the instantiated class, so you modify the class code as such (and I add another function too): 有了更多的经验,您可能希望对实例化的类有一个更明确的引用,因此您可以这样修改类代码(我也添加了另一个函数):

function cPerson(sSetName)
{
  var sName= sSetName;
  var oPersonInst = this;

  this.sMyNameIs = function () {
    return oPersonInst.sName;
  };

  this.sMyFullNameIs = function () {
    return oPersonInst.sMyNameIs();
  };
}

Now we have come accross three different ways of referring to function or variables from within class functions: 现在,我们遇到了从类函数中引用函数或变量的三种不同方式:

return sName;
return this.sName;
return oPersonInst.sName;

Suppose I want to call the sName variable in the instantiated class very specifically, and these example functions will get more and more and even more complex and in depth as development goes on. 假设我想在实例化的类中非常具体地调用sName变量,并且随着开发的进行,这些示例函数将变得越来越复杂和深入。

I think that the first option ('return sName;') is uncertain, as you are not quite sure whether you are referring to the variable in the right targeted instantiated class scope. 我认为第一个选项('return sName;')不确定,因为您不确定自己是否在正确的目标实例化类作用域中引用变量。 It could be an accidental local var that you are calling. 您可能是偶然的本地变量。

The second, using this , IMHO, is also not perfect as this apparently changes depending on situations, and you can't (?) rely on that you are calling the right variable in the instantiated class specifically. 第二种方法,使用this ,恕我直言,也不完美,因为this显然会根据情况而改变,并且您不能(?)依赖于您在实例化类中专门调用了正确的变量。

So the third reference, is IMHO, the best looking reference. 因此,第三个参考是恕我直言,这是外观最好的参考。 Very specifically defined right at instantiation of the class. 在类实例化时非常明确地定义。 How can there be any doubt that the var sName of the class is meant. 毫无疑问,该类的var sName是什么意思。

Q1 : Why are functions in the class defined with this. 问题1 :为什么用this.定义了类中的函数this. ? Can it not better be defined with oPersonInst.something = function () { //... }; 是否可以使用oPersonInst.something = function () { //... };更好地定义它oPersonInst.something = function () { //... }; ?

Q2 : why does, in code that I have given, alert(oPersonInst.sMyNameIs()); 问题2 :为什么在我给出的代码中会发出alert(oPersonInst.sMyNameIs()); work, but alert(oPersonInst.sName); 工作,但alert(oPersonInst.sName); does not (at least, not within $.each( something, function() { //right here }) ), but alert(sName); 不会(至少不在$.each( something, function() { //right here }) )),而是alert(sName); DOES work (not favored IMHO, because of the above mentioned reasons)? 可以工作吗(由于上述原因,我不赞成IMHO)?

Q3 : Can someone pot some exemplary code, or maybe even change this sample code, where solid out-of-local-scope references are used, that will work outside as well as inside $.each(...) callback functions (or other callback related functions) ? 问题3 :有人可以添加一些示例性代码,甚至可以更改此示例代码(使用可靠的局部范围外引用),该示例代码在$.each(...)回调函数的内部和外部起作用(或其他与回调相关的功能)

Forgive my confusion, we're all learners, and I feel a hole in my brain. 原谅我的困惑,我们都是学习者,我感到自己的大脑有一个洞。 This article did not explain my concerns over this very well. 本文没有很好地解释我对此的担忧。

this doesn't change randomly at all.. it always means the current closure (function): 这根本不会随机改变..它始终意味着当前的闭包 (函数):

  • if you are in function_1() -- this means function_1 如果您在function_1()中-这意味着function_1
  • if you are in function_1>function_2 -- function_2 is the current closure and this means function_2 如果您在function_1> function_2中--function_2是当前的闭包,这意味着function_2

IF you need function_1's this in function_2 you have to capture it while in function_1: 如果您需要在function_2中使用function_1,则必须在function_1中捕获它:
meaning var function1This = this; 含义var function1This = this;


and the way to reference a var by using the functions name before it means static access. 以及在表示静态访问之前使用函数名称引用var的方法。 It has different semantics 它具有不同的语义


so use this.var to be sure you get the INSTANCE's variable 因此请使用this.var以确保您获得了INSTANCE的变量
use a 'captured this pointer' to access your parents INSTANCE's variable 使用“捕获此指针”来访问您的父母INSTANCE的变量

and only use name.var if you want to get the static, shared (between all instances) value 如果要获取静态(所有实例之间)共享的值,则仅使用name.var

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM