When someone begins to learn JavaScript, write notes, _javascript skills.

Source: Internet
Author: User
Tags extend inheritance prototype definition
Copy Code code as follows:

/*
* A JavaScript object is a set of properties (methods)
* In this language, if the variable or method name does not conform to the declaration specification,
* You must use the square brackets "[]" to refer to it
*
*/
/**
* <1.> The statement declares a Class1 class, the Class1 equivalent to the constructor method, and also called the constructor
* You can also say that a Class1 method has been declared
*/
function Class1 () {
This.name= "XJL"; Adding attributes to an object
this.say= function () {alert ("Hello everyone!)" ");}; Add a method to an object
};
/**
* <2.> Create instance with the New keyword, the new operator is not only valid for internal classes, but also for user-defined class use
* Each object can be considered as a collection of multiple attributes (methods), that is, the object name. Property (method) or object name [property (method) name]
* square brackets ' [] ' is suitable for uncertain specific reference to which attribute (method) of the occasion
*/
var a = new Class1 ();
Alert (typeof (a)); typeof (A) returns the type of a
alert (a.name); Each object can be viewed as a collection of multiple properties (methods).
Alert (a[' name ')); Use square brackets ([]) to refer to the properties and methods of an object
The dropdown box object name [Dropdown box object. value] Gets the value of the user's choice, as well as the Eval (dropdown box object name.) + Dropdown Box object. Value);
A.say (); Method of calling Object
var arr=new Array ();
arr[' push '] (' abc '); Add an element to an array, where the push is a built-in property
arr[' push '] (' 1234 '); Add an element to an array
Alert (arr);
/**
* < Two .> dynamically add, modify, and delete properties and methods of objects
*
*/
var obj = new Object ();
Add Properties ... Where the property name can be arbitrarily taken
Obj.name= "Xu Jianlong";
Obj.sex = ' Male ';
obj[' My name ' = ' Xujianlong '; Use the square brackets "[]" to use a non-identifier string as the property name
Add Method ... The method name can also be arbitrarily taken, also can pass the parameter
Obj.alert = function (a) {
Alert (A + "Hello!") ");
}
Modifying attributes is to change the value of the property to something else
Obj.name = "John";
obj[' name ' = ' Anme ';
To delete a property, you change the value of the property to undefined or null
Obj.name = ' undefined ';
/**
* < three > Creating untyped objects using curly braces ({}) syntax
*/
In curly braces Chinese properties and methods, attributes are separated by commas, and properties are separated by colons
var ob = {
Name: "123",
Say:function () {alert ("123")}//Last property or method without commas
}
You can also define the properties and methods of an object using the following methods
var ob1 = {"name": ' 123 ', ' Say ': function () {alert ("ABCD");};
/**
*< Four >prototype Prototype object
* All functions (function) corresponding to the class is (function)
* prototype is actually a collection of members that represent a class.
* When you obtain an object of a class by using new, the members of the prototype object become members of the instantiated object.
*/
function Class2 () {//Create an Object
}
var ob2 = new Class2 ();
class2.prototype.me = function () {alert ("123");}//In front of prototype, the class name you created
Class2.prototype.name = "123"; //
/**
* Relationship between the function object and other internal objects
*/
typeof (New Function ()), typeof (function), typeof (Array), typeof (Object) return string "Function" These parameters are called constructors
typeof (New Date ()), typeof (New Array ()), typeof (New Object ()) returns the string "Object"
/**
* The implied parameter passed to the function: arguments, which has the characteristics of an array, but it is not an array and can be accessed by subscript
*/
The arguments contains a parameter callee, which represents a reference to the function object itself, as follows:
var sum=function (n) {
if (1==n)
return 1;
Else
Return N+arguments.callee (n-1);
}
This statement represents a namespace that declares a Namespace1, as follows:
var Namespace1 = new Object ();
Namespace1.class1 = function () {alert ("123");};
var obj1=new namespace1.class1 (); Executes when the page is loaded
/**
* Define class members using the prototype object
*/
Use the prototype property of the function to define a new member for the class after the statement of the instance is created, which will only be valid for the object created later
The constructor () method in prototype is equivalent to the construction method
function Class1 () {
Alert (' ADF ');
}
Class1.prototype.constructor (); Executes when the page is loaded
Simplified with prototype definition
class1.prototype={
Put in some properties or methods
Multiple properties or methods are separated by commas (,)
}
The following code is static methods and properties
Class1.name= "ABC";
Class1.say = function () {/*codes*/}
With the reflection mechanism, you can change the style specified in the element, while the other styles do not change and get the results you want, for example:
function SetStyle (_style) {
Get the interface object to change the style
var element=getelement ();
For (var p in _style) {
ELEMENT.STYLE[P]=_STYLE[P];
}
}
Inheritance can be achieved by copying the prototype of a class to another class, but with a flaw. For example:
function Class4 () {}
//
function Class2 () {
//
//
Class2.prototype=class4.prototype; Implementation of inheritance
CLASS2.PROTOTYPE.F = function () {alert ("a");}
//
When the prototype changes to Class2, the prototype of CLASS4 also changes
Instanceof operator to determine whether an object is an instance of a class, for example:
var a = new Class2 ();
a instanceof Class2; Returns a bool, which is true if the inheriting class in the Class2 of a
A better kind of inheritance
For (var p in Class1.prototype) {
CLASS2.PROTOTYPE[P]=CLASS1.PROTOTYPE[P];
}
Class2.prototype.ma=function () {
alert (123);
}
The prototype of CLASS4 will not change when the Class2 is changed prototype
/**
* The implementation mechanism of class inheritance in prototype-1.3.1 framework
*/
//-------------------------------------------------------------------------------------------
The statement adds a extend method to each object, with the following code;
Object.extend = function (destination, source) {
For (property in source) {
Destination[property] = Source[property]; Assign all properties or methods in source to destination
}
return destination;
}
Add a method to each object by using the object class extend
Object.prototype.extend = function (object) {
Return Object.extend.apply (this, [this, Object]);
}
Object.extend.apply (This,[this,object]);
Class1 inheritance and Class2, the advantage is that the new Class2 () is equivalent to assigning a copy of Class2 prototype to Class1
The prototype changes in the Class1 will not affect the Prototyp in the Class2.
Class1.prototype= (New Class2 ()). Extend ({/*class1 property or method to be added *});
/**
* Only one declared and not implemented method, class with virtual function is called abstract class, abstract class is not instantiated
*/
The virtual method is not required to be declared, but is used directly. These methods will be implemented in the derived class, for example:
function C1 () {}
c2.prototype={
Fun:function () {This.fn ();} Where the FN method is not defined
}
function C2 () {}
C1.prototype= (New C2 ()). Extend ({
Fn:function () {var x = 1;}
});
This.initialize.apply (this, arguments), which passes the arguments to the Initialize method when the object is created
/***
* You can also use try-catch-finally statements in JavaScript to catch exceptions or error messages
* The E in the parentheses of catch (e) is a must e is an object named error
* e=new error (message) to create this object, the description of the exception is used as an attribute message for the Error object,
*/
This code demonstrates an exception thrown
function S (a,b) {
try{
if (b==0)
throw new Error ("Divisor cannot be zero!") ........");
Else
Alert (A/b)
}catch (e) {
document.write (e.message);///obtains arguments in error by message
}
}
Onlaod=s (1,0);

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.