简体   繁体   中英

structure of object oriented js

I am new to object oriented 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. AbstractFacetWidget is also a class which is also extended from 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). what (function ($) means?

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". So extend creates a new object and is set to AjaxSolr.GenericGraphWidget . This way, you can do something similar to inheritance as is common in other OOP languages.

I guess you deleted some parts, so I hope the following holds true for your specific example: The (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 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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