How JavaScript implements Inheritance

Source: Internet
Author: User

The stereotype's ' inherited ' function person (name) {//defines a person's constructor this.name = name;//Add Attribute}person.prototype.showname=function () {// Add method return this.name;}; function Worker (name,job) {person.apply (this,arguments);//attribute ' inheritance ' this.job=job;} Worker.prototype=new person (); The ' inheritance ' of the Method Worker.prototype.showjob=function () {return this.job;}; var p1=new person (' abc '); var w1=new Worker (' hh ', ' Chef ');

  

(i) A href= "javascript:;"

(1) Don't do anything after clicking on the link

<a href= "javascript:;" class= "" > Mobile registration </a>

Action: Let the Click Effect execute JavaScript Empty statement, prevent click Hyperlink tag to jump.

(2) After clicking on the link, respond to user-defined click events

<a href= "javascript:void ()" class= "" > Mobile registration </a>

(b) $ (this). Index ()

Function: Returns the location of its own

(c) toggleclass ()

$element. Toggleclass (ClassName);//equivalent to if ($element. Hasclass (ClassName)) {    $element. removeclass (className);} else{    

There are often multiple classes used in real-world development.

. Toggleclass ("none Block");

Function: Toggles the specified CSS class name classnames. If the specified class name exists, the class name is removed and if it does not exist, the class name is added.

(iv). focus () and. Blur ()

Focus event triggering the input field

Blur event triggering the input domain

It probably means: Click in the input field to get the focus. Then click on the input outside the domain to lose focus.

( V) (function ($) {...}) (JQuery) is actually a self-invoking anonymous function

"Need to combine another article": http://www.cnblogs.com/hewasdrunk/p/7457634.html

Function (ARG) {...}
This defines an anonymous function with a parameter of ARG

when calling a function, the parentheses and arguments are written after the function, and because of the precedence of the operator, the function itself needs parentheses, namely:
(function (ARG) {...}) (param)
This is equivalent to defining an anonymous function with a parameter of ARG, and calling this anonymous function as a parameter param
Equivalent:

Funtion Outputfun (ARG) {alert (arg);};

Outputfun ("param");


and (function ($) {...}) (jquery) is the same, the reason why only use $ in formal parameters is to not conflict with other libraries, so the argument with JQuery
var fn = function ($) {...};
FN (jQuery);

The same

You can actually understand that, but be aware that FN doesn't exist.
That function is defined directly and then runs. "Compression" looks like this.
(function ($) {...}) (JQuery)
**************************************************************************
Simple to understand is (function ($) {...}) (jQuery) defines a number of functions that need to be pre-defined
$ (function () {}) is used to run \ Execute those pre-defined functions after the DOM load is complete.
**************************************************************************


(vi) jquery $.fn

$.fn refers to the jquery namespace, plus the methods and properties on the FN, which are valid for each jquery instance.
such as extension $.fn.abc (), or $.FN.ABC () is an ABC method for jquery extension, then each of your jquery instances can refer to this method.
You can do this: $ ("#div"). ABC ();

Jquery.fn = Jquery.prototype

jquery has two methods for developing plug-ins, namely:


One is the development of a class-level plug-in that adds a new global function to jquery, which is equivalent to adding a method to the jquery class itself. The global function of jquery is a function that belongs to the jquery namespace.

The other is the object-level plug-in development that adds methods to jquery objects.

Jquery.extend (object); To extend the jquery class itself. Adds a new method to the class.
JQuery.fn.extend (object); Adds a method to a jquery object. The node object extension method for the specific query

(6.1) Jquery.extend (object); Adding a class method to the jquery class can be understood as adding a static method. Such as:

Adds a "static method" for jquery to add, which can then be used where jquery is introduced,
$.add (3,4); Return 7

(6.2) JQuery.fn.extend (object); The extension to Jquery.prototype is to add "member functions" to the JQuery class. An instance of the jquery class can use this "member function".

(vii) var $this = $ (this);

$this is just a custom variable, $ (this) is the conversion of the current DOM object into a jquery object (also expressed as this refers to: The current DOM object, which is a collection .) )

var $this = $ (this); //This line transforms the DOM object into a jquery object through the $ (DOM object) method and assigns a value to the custom variable of $this

Variable naming rules: Must start with a letter, underscore, or $; so $this is a variable.

Naming with $ starts just to make it easy for code readers to distinguish between jquery objects and other ordinary element objects.

"Need to combine another article": http://www.cnblogs.com/hewasdrunk/p/7457634.html

(eight) options parameter problem of function in Jquery

(options: Select items; defaults: Default values )

"Need to combine another article": http://www.cnblogs.com/hewasdrunk/p/7457634.html

no argument is undefined , at the same time

The general jquery plugin uses an Options object as a parameter. At the beginning of the plugin, however, the following code completes the default configuration:

Options = $.extend (True, {    //default configuration}, Options | | {});

Reference:

$.fn.bgchange = function (options) {  //given a default value.     var defaults = {          color: "Black", FontSize: "N"      },//replace the passed value with the default value (if options are empty, use defaults, Can avoid passing options that are null and do not pass the options case)
var options = $.extend (defaults,options);
Plugin content is written here
Return This.css ({
' Color ': Options.color,
' FontSize ': options.fontsize
});

Our plug-in can be called this: the font size is not specified, will be used in the plug-in default value of 12px.

$ ("a"). Bgchange ({   color: "Blue"})

exposing plug-in default settings , Purpose: easy to change plugins

$.fn.bgchange = function (options) {      var options = $.extend ({}, $options);  }; $.fn.bgchange.default={    Color: "Black", FontSize: "A"  }

Now the user can include such a line of code in their script:

This only needs to be called once, and does not necessarily call $.fn.bgchange.defaults.color= "blue" in the Ready Block;

Next we use the plug-in method like this, the result it sets the Blue font color:

$ ("a"). Bgchange ();

(ix) Use of $.extend (OBJ1,OBJ2) for substitution

For Jquery.extend (CSS1,CSS2), for example, CSS1,CSS2 has some attributes (the law will still be better than processing, here's the speaking of attributes).
The Extend function adds a property that CSS2 has and css2 not to Css1, and if a property of Css2 is enjoyed with a Css1 property name, the Css2 property is overwritten with the Css1 's attribute of the same name. Css1 is the final integer and object. Or you can also use:
var newcss = jquery.extend (CSS1,CSS2) NEWCSS is a new object that is merged.
var newcss = jquery.extend ({},css1,css2) newcss is a new object that is merged. and does not destroy the structure of CSS1.

Usage: jquery.extend (obj1,obj2,obj3,..) 2var css1={size: "10px", Style: "Oblique"}3var css2={size: "12px", Style: "Oblique", Weight: "Bolder"}4$.jquery.extend ( CSS1,CSS2) 5//Result: The Size property of Css1 is overridden and inherits the weight property of Css2 6//Css1 = {size: "12px", Style: "Oblique", Weight: "Bolder"}7

The new extend () allows you to merge nested objects more deeply. The following example is a good proof.

Previous. Extend ()    2   jquery.extend (    3     {name: "John", Location: {city: ' Boston '}},    4 {Last     : "Res IG ", Location: {state:" MA "}}    5   );    6    //Result:    7    //+ = {name: ' John ', Last: ' Resig ', location: {state: ' MA '}} 8  //new deeper. Extend () 
   9   Jquery.extend (True,   ten   {name: "John", Location: {City: "Boston"}},   each     {last: "Resig" , location: {state: "MA"}}  );   //  Results   //+ = {name: "John", Last: "Resig", +   /  /location      : {City: "Boston", state : "MA"}}  16

(10) JS | | The meaning of js in o = O | | {}; What does it mean?

o = O | | {} means: If O is null or undefined, then O Initializes an empty object (that is, {}), otherwise o is unchanged. The goal is to prevent O from being null or undefined errors.

Where: | | Representation or action, the first condition is true, the result is true without the need to execute the second condition, otherwise the second condition is executed, equivalent to the following code:

if (o)

o = o;
Else
o = {};

How JavaScript implements Inheritance

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.