简体   繁体   English

在 Javascript 中使用实例 - @class、@param、@property、@augments

[英]Using instances in Javascript - @class, @param, @property, @augments

I got a javascript file.我有一个 javascript 文件。 There are 3 classes and I need to create these classes instances and use the methods they contain.有 3 个类,我需要创建这些类实例并使用它们包含的方法。 Problem is, these 3 classes are not like class Example {...} .问题是,这 3 个类不像class Example {...} Instead they look like this:相反,它们看起来像这样:

* @class
* @param {Array|Object} param1 - [REQUIRED]
* @param {Array|String} param2 - [OPTIONAL]
* @property {String} prop1
* @property {String} prop2
* @property {Array} prop3
* @property {Class2} prop4

function Class1(param1, param2) {
    ...
}

@augments Class1
@param {String} param5
@return {String}

Class1.prototype.someName = function (param5) {
    ...
}

And it goes like this.它是这样的。 My questions are:我的问题是:

1) What does @class or property etc. mean? 1) @classproperty等是什么意思?

2) What is difference between func Class1 and Class1.prototype.someName ? 2) func Class1Class1.prototype.someName什么区别?

3) How can I create instance from these 3 classes and use the methods from another js file. 3)如何从这 3 个类创建instance并使用另一个 js 文件中的方法。 Because I need to create everything from this javascript file.因为我需要从这个 javascript 文件中创建所有内容。 They contain some HTML with CSS classes like:它们包含一些 HTML 和 CSS 类,例如:

function Class1(param1, param2) {
    this.openTag;
    this.closeTag;
    this.htmlTags;

    this.openTag = '<div id="' + this.elementId + '">';
    this.closeTag = '</div>';

    this.htmlTags = {
        sectionTitle: {
            openTag: '<h2 class="class1">',
            closeTag: '</h2>'
        },
        group: {
            openTag: '<div class="class2">',
            closeTag: '</div>'
        },
        table: {
            openTag: '<table class="class3">',
            closeTag: '</table>'
        }
    }
   ...
}

How can I create an instance of these classes and call them from another javascript file?如何创建这些类的实例并从另一个 javascript 文件中调用它们? When I try to do ES6 imports/exports, it gives me this error:当我尝试进行 ES6 导入/导出时,它给了我这个错误:

Access to script at 'file:///Users/user/Desktop/project/someName.js' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

& &

app.js:1 Uncaught SyntaxError: Cannot use import statement outside a module

It doesn't allow me to call functions from another js file.它不允许我从另一个 js 文件调用函数。

I would appreciate if you explain everything step by step:)如果您逐步解释所有内容,我将不胜感激:)

What does @class mean? @class是什么意思?

This is something called JSDoc.这就是所谓的 JSDoc。 It's basically the equivalent of the summary xml comments for c#.它基本上等同于 c# 的摘要 xml 注释。 At a high level, it's just a better way to document your methods, classes, variables, functions, etc... rather than just doing //... everywhere.在高层次上,它只是记录您的方法、类、变量、函数等的一种更好的方式……而不是到处都做//... You can learn more about JSDoc here .您可以在此处了解有关 JSDoc 的更多信息。 Other languages have similar things.其他语言也有类似的东西。 I am most familiar with c# and JS though.不过,我最熟悉 c# 和 JS。

The cool thing about JSDoc is that if you have an IDE that supports it, you can basically just hover over something and assuming you've got proper JSDoc on whatever module you're using, you'll get the documentation right then and there. JSDoc 最酷的地方在于,如果你有一个支持它的 IDE,你基本上可以只用 hover 来处理某些东西,并且假设你在使用的任何模块上都有正确的 JSDoc,那么你就会立即获得文档。 You don't have to hop over to the source to see if the author left any comments for you.你不必跳到源头看看作者是否给你留下了任何评论。 They'll just pop up inline.他们只会弹出内联。 WebStorm does a good job with this. WebStorm 在这方面做得很好。

What is the difference between func Class1 and Class1.prototype.someName func Class1Class1.prototype.someName有什么区别

This (IMO) is the old school way of writting classes in JavaScript.这(IMO)是在 JavaScript 中编写课程的老派方式。 With ES6+, you can just use the keyword class instead of function and having to use prototypes.使用 ES6+,您可以只使用关键字class而不是function并且必须使用原型。

Essentially,本质上,

function Class1 (...) {...}

is the older way of doing是旧的做事方式

class Class1 {... }

With that being said,话虽如此,

Class1.prototype.someName = function (...) {... }

is the old school way of doing是老派的做法

class Class1 () {
    constructor(...) {...}
    someName(...) { ... }

}

For your 3rd question, I am unclear what you're asking.对于你的第三个问题,我不清楚你在问什么。

Example of using modern day classes vs the 'older' way:使用现代课程与“旧”方式的示例:

 // Older way of creating a Person class function Person(first, last) { this.first = first; this.last = last; } Person.prototype = { getFullName: function () { return this.first + ' ' + this.last; } } var oldPerson = new Person('John', 'Doe'); console.log(oldPerson.getFullName()); // Modern way of creating a class class Person1 { constructor(first, last) { this.first = first; this.last = last; } getFullName() { return `${this.first} ${this.last}`; } } const newPerson = new Person1('Jane', 'Doe'); console.log(newPerson.getFullName());

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

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