在JavaScript中嵌套类和该值

在JavaScript中嵌套类和该值,javascript,class,nested,this,prototype,Javascript,Class,Nested,This,Prototype,我可以用Javascript编写嵌套类吗 function A() { this.a; this.B = function() { this.ab ; this.C = function() { this.ab = 0; } } } 如果上述代码正确,则 1.How do I declare an object of type B 2.Whose property is ab.A() 's

我可以用Javascript编写嵌套类吗

function A()
{
    this.a;
    this.B = function()
    {
      this.ab ;
      this.C  = function()
      {
         this.ab = 0;
      }
    }
}
如果上述代码正确,则

1.How do I declare an object of type B 2.Whose property is ab.A() 's or B() 's?. 3.Inside B() where does the 'this' points to.To A() Or to B()? 1.如何声明类型为B的对象 2.谁的属性是ab.A()或B()?。
3.在B()内部,“this”指向哪里?指向A()还是指向B()?这是非常非正统的,但是没有什么可以阻止您在JavaScript中嵌套构造函数

从您的示例中,您可以从A的实例访问B函数:

var someA = new A();
var someB = new someA.B();
new new A().B  // means => new ( new A ).B  
要回答您的另一个问题:

 this.B = function() {
      this.ab = 0;
      this.c  = function() {
         this.ab++;
      }
    }
在B内部所指的内容取决于如何调用
B
。如果调用
B
作为构造函数,使用
new
关键字,
this
将是从B的原型继承的新对象

如果在不使用
new
的情况下调用B,它将被视为一个方法,
将是调用该方法的a的实例

以此类推,如果用
new
调用C,则C内部的
将是从C的原型继承的新对象。或者C可以是B的一种方法,这更有意义。这就是你想要的东西:

function A() {
    this.a;
    this.B = function() {
         this.ab = 0;
         this.c  = function() {
             this.ab++;
         }
    }
}

var someA = new A();
var someB = new someA.B();

console.log(someB.ab); //0
someB.c();
console.log(someB.ab); //1


最后,请注意,尽管这样的嵌套构造函数并不常见,但没有什么能阻止您像添加任何其他构造函数一样添加到
B
的原型中

function A() {
    this.a;
    this.B = function() {
      this.ab = 0;
      this.c  = function() {
         this.ab++;
      }
    }
    this.B.prototype.foo = function() { alert("Bar " + this.ab) };
}

var someA = new A();
var someB = new someA.B();

console.log(someB.ab);
someB.c();
console.log(someB.ab);
someB.foo();   //Bar 1

在您的示例中,“类”是特定于实例的。你确定你想要吗?您可能正在寻找以下内容:

function A() {
    // ...
}

A.B = function() {
    // ...
};

var one = new A();
var two = new A.B();
尽管“嵌套”类将无法访问“私有成员”,因为JavaScript首先没有这些成员


至于你的例子:

  • 您可以创建A的一个实例,比如说
    newa()
    ,访问
    B
    ,比如说
    newa().B()
    1,或者用变量替换
    newa()
  • 都不是,现在这是一个空的声明。。。但它将是
    B
    实例的一个属性
  • B
    的实例(除非使用了
    Function.call
    Function.apply

  • 1是的,它有效

    摘要:

    不使用
    new
    运算符调用函数意味着
    将引用创建该函数的对象。 对于全局变量,此对象是窗口对象。 使用
    new
    调用-函数的行为类似于类中的构造函数,
    引用将创建的此实例

    1.如何声明类型为B的对象

    第一种方法(出于好奇)-通过调用
    a
    而不使用
    new
    操作符
    将引用
    window
    对象和
    B
    方法,以及使用
    this
    声明的所有其他内容泄漏到全局范围,因为
    a==window.a
    =>true

    A(); 
    var b = new B; // means => new window.B  //parentheses can be ommited if you invoking without arguments  
    alert( ab )  // alerts 'inside A'  -> value from code presented below
    
    或来自以下实例:

    var someA = new A();
    var someB = new someA.B();
    
    new new A().B  // means => new ( new A ).B  
    
    小心点

    2.谁的属性是ab.A()或B()?

    如上所述,这取决于我们将如何访问它:

     function A()
        {
            this.ab = "inside A";
            this.B = function()
            {
              this.ab = "inside B";
              this.c  = function()
              {  
                 this.ab = "inside C";
              }
            }
        };
    
    看看这个

    var a = new A;
    a.ab // "inside A"
    a.B(); // in B now 'this' refers to 'a', 'a.ab' will be replaced to 'ab' from inside 'B'
    a.ab // "inside B"
    
    但是

    3.B()内部的“this”指向哪里。指向A()还是指向B()?


    如上所述:)

    哪个对象在B()中被“this”引用。A的对象或B的对象?我可以像函数A()一样使用“this”{this.A;this.B=function(){this.ab;this.C=function(){this.ab=0;//这是指B this.A=1;//这是指A}}}明白了..谢谢亚当Rackis@JinuJD-当然。为了完整起见,我又添加了一个editJavaScript,它没有类,只有对象和函数。