简体   繁体   English

面向对象js的结构

[英]structure of object oriented js

I am new to object oriented js.我是面向对象 js 的新手。 In my work, i have to edit some previous code done by some other person.在我的工作中,我必须编辑一些其他人以前完成的代码。 AS i am new to this area, can not understand some structure and notation.因为我是这个领域的新手,无法理解一些结构和符号。 Bellow the structure that i can not deal with,波纹管我无法处理的结构,

(function ($) {
    AjaxSolr.GenericGraphWidget = AjaxSolr.AbstractFacetWidget.extend({

      _functionName:function(){
       }

   })(jQuery);

Now here AjaxSolr is a base class which is defined in another file.现在这里AjaxSolr是一个在另一个文件中定义的基类。 AbstractFacetWidget is also a class which is also extended from AjaxSolr . AbstractFacetWidget也是一个类,它也是从AjaxSolr扩展而来的。 Now can anybody explain, what kind of structure is this?(ya, i understand only that this is a kind of class, like other oop language, which is extended from another class).现在有人可以解释一下,这是一种什么样的结构?(是的,我只明白这是一种类,就像其他 oop 语言一样,是从另一个类扩展而来的)。 what (function ($) means?什么(function ($)是什么意思?

What i know untill now that to create object and inheritance i have to do like the following-直到现在我才知道要创建对象和继承,我必须像以下那样做-

var Constructor = function(name) {
    this.name = name
};

Constructor.prototype.mymethod = function() {
    alert("my name is : " + this.name);
};

var obj = new Constructor("foo");
obj.mymethod();

May be they used another format that i don't know.可能他们使用了我不知道的另一种格式。 If ,I want to call a function of that class from outside of that class, how can i do this?如果,我想从该类的外部调用该类的函数,我该怎么做? If my question does not explain well, please ask me.如果我的问题没有解释清楚,请问我。

Usually, .extend({...}) means "take their prototype and merge the supplied object".通常, .extend({...})意思是“获取他们的原型并合并提供的对象”。 So extend creates a new object and is set to AjaxSolr.GenericGraphWidget .所以extend创建了一个新对象并设置为AjaxSolr.GenericGraphWidget This way, you can do something similar to inheritance as is common in other OOP languages.这样,您可以执行类似于其他 OOP 语言中常见的继承的操作。

I guess you deleted some parts, so I hope the following holds true for your specific example: The (function ($) { ... })(jQuery);我猜你删除了一些部分,所以我希望以下内容适用于你的具体例子: (function ($) { ... })(jQuery); is a way to write an immediately invoked function expression: It creates an anonymous function ( function ($) { ... have $ available here ... } ) and calls it directly with some argument (in this case jQuery ).是一种编写立即调用的函数表达式的方法:它创建一个匿名函数( function ($) { ... have $ available here ... } )并使用一些参数(在本例中为jQuery )直接调用它。

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

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