Home  >  Article  >  Web Front-end  >  JavaScript fast charging

JavaScript fast charging

高洛峰
高洛峰Original
2016-11-25 14:03:001204browse

Invented by Netscape, the idea comes from the functional language Schema and Self, a bit close to Python.

Features: weak typing, dynamic parsing, functions are objects, objects are based on Prototype

The first three features are easy to understand, and the last one needs to be explained in detail.

The most basic best practice: use Functions and objects in large programs! Don’t do journal programming.

The easiest way to create an object is:

var myObject = new Object();

JavaScript objects are essentially associative arrays.

Tips: Pay attention to the difference between using functionName() and functionName when assigning a function to a variable. The former pays the result of the function to the variable, while the latter pays the function reference to the variable.

You can use JSON to create and modify JavaScript objects, and JavaScript can also modify objects created by JSON.

Constructor/class/prototype attribute

JavaScript also has the concepts of objects and classes, but there is no concept of built-in inheritance. In fact, every JavaScript object is an instance of the same base class. This base class has the ability to tie member fields and functions to itself at runtime.

Safe alternative: prototype attribute

prototype is a property of JavaScript objects that has no equivalent in OO languages. Functions and properties can be associated with a constructor prototype. Then the prototype and new keywords work together. When a function is called using new, all properties and methods of the function prototype will be attached to the result object.

Java code

function MyObject(name, size){

this.name = name;

this.size = size;

}

MyObject.prototype.tellsize = function(){

alert ("size of "+this.name+" is "+this.size);

}

var myObj = new MyObject("tiddles", "7.5 meters");

myObj.tellSize();

function MyObject(name, size){

this.name = name;

this.size = size;

}

MyObject.prototype.tellsize = function(){

alert("size of "+this.name+" is "+this.size);

}

var myObj = new MyObject("tiddles", "7.5 meters");

myObj.tellSize();

Note that we can only reference the prototype after declaring the constructor. The object inherits things that have been attached to the prototype before calling the constructor. The prototype can be modified between calls to the constructor, and anything, not just functions, can be attached to the prototype.

Using prototypes to define class-like behavior for JavaScript objects is a safe and reliable path.

Extend built-in classes

In web browsers, some core classes can be extended through the prototype mechanism.

Java code

Array.prototype.indexOf = function(obj) {

var result = -1;

for (var i=0; i< this.length; i++) {

if (this[i ]==obj){

result=i;

break;

}

}

}

Array.prototype.indexOf = function(obj) {

var result = -1;

for (var i=0; i< this.length; i++) {

if (this[i]==obj){

result=i;

break;

}

}

}

Prototype inheritance

JavaScript has no native implementation, but there are some neat workarounds.

Reflection of JavaScript objects

is used to discover the type of the object

Event handling and function context

When declaring the event handler function of the Dom element programmatically, even if no parameters are assigned to the function, when the Dom element is clicked When, the Event object is used as the parameter of the function call, and the element itself is used as the context object.

If you want the event handler function to refer to the model object it is attached to, there are two ways:

1. Reference the model using the name

2. Attach the model to the Dom node


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn