为什么当JS不进行时,为什么DOM面向对象结构?

发布于 2025-02-02 13:23:01 字数 630 浏览 3 评论 0原文

在DOM(HTML解析文件的js rappresentation)中,我们具有此结构(其中< 用于“ senasented ”):

   Object(JS object) < Node < Element < HTMLElment < HTMLBodyElement, HTMLDivElement, etc..
   Object(JS object) < Node < DOcument < HTMLDocument
   ...

所有这些节点均称为接口( https://develper..mozilla.mozilla.org/en-us/en-us/docs/web/api/ htmlelement )在我检查的每个文档中。

我不明白JS中的这种情况如何(不是面向对象,没有接口等)?我有C ++的背景,因此也许我将DOM“面向对象”的结构与真实的结构混淆。有澄清吗?

In DOM (JS rappresentation of HTML parsed file) we have this structure(where < is for "inherited"):

   Object(JS object) < Node < Element < HTMLElment < HTMLBodyElement, HTMLDivElement, etc..
   Object(JS object) < Node < DOcument < HTMLDocument
   ...

All these nodes are called interfaces (https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement) in every documentation I have checked.

I don't understand how is this possible in JS (not object-oriented, no interfaces, etc)? I have a background in C++, so maybe I am confusing DOM "object-oriented" structure with a real one. Any clarification?

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

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

发布评论

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

评论(1

花心好男孩 2025-02-09 13:23:01

我不明白JS中的可能性如何(不是面向对象,没有接口等)?

在JavaScript中建模DOM的结构没有问题。 JavaScript 非常面向对象,包括支持继承。 javaScript使用原型遗传。例子:

// Create an object with a method
const base = {
    baseMethod() {
        console.log("baseMethod");
    }
};

// Create an object using `base` as its prototype,
// perhaps add a property to it
const sub = Object.assign(Object.create(base), {
    subMethod() {
        console.log("subMethod");
    }
});

// Sub inherits `base`'s properties
sub.baseMethod(); // "baseMethod"

// And has its own
sub.subMethod();  // "subMethod"

JavaScript还覆盖了似乎的原型继承之上的东西,而是class like(不是,它仍然是典型的,但是对于来自基于类的人来说,它更舒适语言)。例如,这是一个三层继承模型(类似于node&lt; element&lt; htmlelement)使用class> >语法(但同样,我们也可以在没有class语法的情况下完成所有这些操作):

class Base {
    baseMethod() {
        console.log("baseMethod");
    }
}

class Sub extends Base {
    subMethod() {
        console.log("subMethod");
    }
}

class SubSub extends Sub {
    subSubMethod() {
        console.log("subSubMethod");
    }
}

const x = new SubSub();
x.baseMethod();                     // "baseMethod"
x.subMethod();                      // "subMethod"
x.subSubMethod();                   // "subSubMethod"
console.log(x instanceof Base);     // true
console.log(x instanceof Sub);      // true
console.log(x instanceof SubSub);   // true
console.log(x instanceof Date);     // false (just to show it's not always true ;-) )

对象该结构的基础是隐式的。)

I don't understand how is this possible in JS (not object-oriented, no interfaces, etc)?

There's no problem modelling the DOM's structure in JavaScript. JavaScript is very object-oriented, including supporting inheritance. JavaScript uses prototypical inheritance. Example:

// Create an object with a method
const base = {
    baseMethod() {
        console.log("baseMethod");
    }
};

// Create an object using `base` as its prototype,
// perhaps add a property to it
const sub = Object.assign(Object.create(base), {
    subMethod() {
        console.log("subMethod");
    }
});

// Sub inherits `base`'s properties
sub.baseMethod(); // "baseMethod"

// And has its own
sub.subMethod();  // "subMethod"

JavaScript also overlays something on top of prototypical inheritance that seems rather class-like (it isn't, it's still prototypical, but it's more comfortable for people coming from class-based languages). For instance, here's a three-layer inheritance model (rather like Node < Element < HTMLElement) using class syntax (but again, we could do all of this without class syntax, too):

class Base {
    baseMethod() {
        console.log("baseMethod");
    }
}

class Sub extends Base {
    subMethod() {
        console.log("subMethod");
    }
}

class SubSub extends Sub {
    subSubMethod() {
        console.log("subSubMethod");
    }
}

const x = new SubSub();
x.baseMethod();                     // "baseMethod"
x.subMethod();                      // "subMethod"
x.subSubMethod();                   // "subSubMethod"
console.log(x instanceof Base);     // true
console.log(x instanceof Sub);      // true
console.log(x instanceof SubSub);   // true
console.log(x instanceof Date);     // false (just to show it's not always true ;-) )

(The Object base of that structure is implicit.)

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