JavaScript 中类的嵌套和 this 值

发布于 2024-12-24 00:27:35 字数 380 浏览 5 评论 0 原文

我可以用 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()?

Can I write nested classes in Javascript?

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

If the above code is correct,then

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()?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ヅ她的身影、若隐若现 2024-12-31 00:27:35

在您的示例中,“类”将是特定于实例的。你确定你想要那个吗?您可能正在寻找更多类似的内容:

function A() {
    // ...
}

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

var one = new A();
var two = new A.B();

尽管“嵌套”类无法访问“私有成员”,因为 JavaScript 一开始就没有这些成员。


至于您的示例:

  1. 您将创建 A 的实例,例如 new A(),并访问 B,例如 new new A().B() 1,或用变量替换new A()
  2. 两者都不是,目前它是一个空语句...但它将是 B 实例的属性。
  3. B 的实例(除非使用 Function.callFunction.apply)。

1 是的,它有效!

In your example, the "classes" will be instance-specific. Are you sure you want that? You might be looking for something more along the lines of:

function A() {
    // ...
}

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

var one = new A();
var two = new A.B();

Although the "nested" classes won't be able to access "private members" because JavaScript doesn't have those in the first place.


As for your example:

  1. You would create an instance of A, say new A(), and access B, say new new A().B()1, or replacing new A() with a variable.
  2. Neither, it's an empty statement for now... but it would be a property of the B instance.
  3. To an instance of B (unless Function.call or Function.apply is used).

1 Yes, it works!

倚栏听风 2024-12-31 00:27:35

摘要:

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

1.如何声明 B 类型的对象< /strong>

第一种方法(出于好奇) - 通过调用 A 而不使用 new 运算符 this 将引用 window< /code>object 和 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

或来自 A 的实例:

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"

var a = new A;
a.ab // "inside A"
var b = new a.B;
a.ab // "inside A"
b.ab // "inside B"

// and now
b.c() 
b.ab // "inside C" and so on:)  

3.在 B() 内部,'this' 指向哪里。To A() 还是 B()?

如上:)

Abstract:

Calling to function without using new operator means that this will be refer to object in which that function was created.
For global variables this object is window object.
Calling using new - function behave as constructor like in classes and this refers to this instance which will been created

1.How do I declare an object of type B

First way( as a curiosity ) - by calling to A without using new operator this will be refer to windowobject and B method and all other what was declared with this leaks to global scope because 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

or from instance of A:

new new A().B  // means => new ( new A ).B  

Be careful.

2.Whose property is ab.A() 's or B() 's?.

As above, it depends of how we'll accessing to it:

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

Check this out

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"

but

var a = new A;
a.ab // "inside A"
var b = new a.B;
a.ab // "inside A"
b.ab // "inside B"

// and now
b.c() 
b.ab // "inside C" and so on:)  

3.Inside B() where does the 'this' points to.To A() Or to B()?

As above:)

你的他你的她 2024-12-31 00:27:35

这是非常不正统的,但是没有什么可以阻止你在 JavaScript 中嵌套构造函数。

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

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

回答您的其他问题:

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

B 内部的 this 指的是什么取决于 B 如何 >调用。如果您将 B 作为构造函数调用,并使用 new 关键字,this 将是一个继承自 B 原型的新对象。

如果您在没有 new 的情况下调用 B,它将被视为方法,并且 this 将是调用该方法的 A 的实例。

C 也是如此。如果用 new 调用 C,C 内部的 this 将是一个继承自 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

DEMO


最后,请注意,虽然像这样的嵌套构造函数并不太常见,没有什么可以阻止您添加到 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

更新了演示

It's very unorthodox, but there's nothing stopping you from nesting constructor functions in JavaScript.

From your example, you can access the B function from an instance of A:

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

To answer your other question:

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

What this refers to inside of B depends on how B is invoked. If you call B as a constructor, with the new keyword, this will be a new object inheriting from B's prototype.

If you call B without new, it will be treated as a method, and this will be the instance of A on which the method was called.

And so on with C. If C is called with new, this inside of C will be a new object inheriting from C's prototype. Or C can be a method of B, which makes a lot more sense. Is something like this what you're wanting:

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

DEMO


Finally, note that, though nesting constructors like this isn't too common, there's nothing stopping you from adding to B's prototype just like you would any other constructor

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

Updated Demo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文