Learning notes written by someone while learning javascript _ javascript tips-js tutorial

Source: Internet
Author: User
A JavaScript Object is a set of attributes (methods). In this language, if the variable name or method name does not comply with the Declaration specification, you must reference it with square brackets "[]". The Code is as follows:


/*
* JavaScript objects are a set of attributes (methods ).
* If the variable or method name does not comply with the Declaration Rules in this language,
* You must reference it with square brackets [].
*
*/
/**
* <1.> This statement declares a class1 class. class1 is equivalent to the constructor.
* You can also declare a class1 method.
*/
Function class1 (){
This. name = "xjl"; // Add attributes to the object
This. say = function () {alert ("Hello! ") ;}; // Add a method to the object
};
/**
* <2.> The new Keyword is used to create an instance. The new operator is not only valid for internal classes, but also for user-defined classes.
* Each object can be considered as a collection of multiple attributes (methods), that is, the object name. Attribute (method) Name or object name ["attribute (method) Name"]
* Square brackets '[]' are applicable when you are not sure which attribute (method) to reference.
*/
Var a = new class1 ();
// Alert (typeof (a); // typeof (a) returns the type of
// Alert (a. name); // each object can be considered as a set of multiple attributes (methods,
// Alert (a ['name']); // uses square brackets ([]) to reference object attributes and Methods
// The drop-down box object name [drop-down box object. value] can be used to obtain the value selected by the user ("drop-down box Object Name." + drop-down box object. value );
// A. say (); // call the object Method
// Var arr = new Array ();
// Arr ['push'] ('abc'); // Add an element to the array. The push is a built-in attribute.
// Arr ['push'] ('20140901'); // Add an element to the array
// Alert (arr );
/**
* <二.> Dynamic addition, modification, and deletion of Object Attributes and Methods
*
*/
Var obj = new Object ();
// Add attributes ...... The attribute names can be arbitrary.
Obj. name = "Xu jianlong ";
Obj. sex = 'male ';
Obj ['myname'] = "xujianlong"; // use square brackets "[]" to use a non-identifier string as the property name
// Add method ...... The method name can also be any or any parameter.
Obj. alert = function (){
Alert (a + "Hello! ");
}
// Modify the attribute to change the attribute value to another content.
Obj. name = "James ";
Obj ['name'] = 'anme ';
// Delete an attribute. The attribute value is changed to undefined or null.
Obj. name = 'undefined ';
/**
* <三> Use the braces ({}) syntax to create a non-type object
*/
// Chinese attributes and methods in braces. attributes and attributes are separated by commas, and attributes and values are separated by colons.
Var ob = {
Name: "123 ",
Say: function () {alert ("123")} // do not use commas (,) for the last attribute or method.
}
// You can use the following method to define the attributes and methods of an object:
Var ob1 = {"name": '000000', 'server': function () {alert ("abcd ");}};
/**
* <四> Prototype object
* All functions correspond to the (function) class)
* Prototype is actually a set of class members.
** When a new class object is obtained, members of the prototype object will become members of the instantiated object.
*/
Function class2 () {// create an object
}
Var ob2 = new class2 ();
Class2.prototype. me = function () {alert ("123");} // The Name Of The class you created before prototype
Class2.prototype. name = "123 ";//
/**
* Relationship Between Function objects and other internal objects
*/
// Typeof (new Function (), typeof (Function), typeof (Array), typeof (Object) returns the string "function". These parameters are called constructors.
// Typeof (new Date (), typeof (new Array (), typeof (new Object () returns the string "object"
/**
* Implicit 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.
*/
// Arguments contains a parameter callee, which indicates 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 declares a namespace1 namespace, as follows:
Var namespace1 = new Object ();
Namespace1.class1 = function () {alert ("123 ");};
Var obj1 = new namespace1.class1 (); // run the command when the page is loaded.
/**
* Define class members using prototype objects
*/
// Use the prototype attribute of the function to define a new member for the class after the statement for creating the instance. This attribute is only valid for the objects created later.
// Constructor () method in prototype, equivalent to constructor ()
Function class1 (){
// Alert ('afd ');
}
// Class1.prototype. constructor (); // executed when the page is loaded
// Simplified definition with prototype
Class1.prototype = {
// Put some attributes or methods
// Multiple attributes or methods are separated by commas (,).
}
// The following code indicates static methods and attributes.
Class1.name = "abc ";
Class1.say = function () {/* codes */}
// The reflection mechanism can be used to change the style specified in the element. other styles are not changed and the expected results are obtained, for example:
Function setStyle (_ style ){
// Obtain the interface object for style change
Var element = getElement ();
For (var p in _ style ){
Element. style [p] = _ style [p];
}
}
// Inheritance can be realized by copying the prototype of a class to another class, but there are defects. For example:
// Function class4 (){}
//
// Function class2 (){
//
//
// Class2.prototype = class4.prototype; // implement inheritance
// Class2.prototype. f = function () {alert ("");}
//
// When prototype is changed for class2, 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. If the inherited class in class2 of a is true
// A better inheritance
For (var p in class1.prototype ){
Class2.prototype [p] = class1.prototype [p];
}
Class2.prototype. ma = function (){
Alert (123 );
}
// When prototype is changed for class2, prototype of class4 will not change
/**
* Class inheritance implementation mechanism in prototype-1.3.1 framework
*/
// Configure //-------------------------------------------------------------------------------------------
// This statement adds an extend method to every object day. The Code is as follows;
Object. extend = function (destination, source ){
For (property in source ){
Destination [property] = source [property]; // assign all the attributes or methods of source to destination
}
Return destination;
}
// Use the Object class to add the extend method for each Object
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 the prototype copy of class2 to class1
// Prototype changes in class1 will not affect prototyp in class2
Class1.prototype = (new class2 (). extend ({/* attributes or methods to be added to class1 */});
/**
* Only a declared but not implemented method is used. classes with virtual functions are called abstract classes. abstract classes cannot be instantiated.
*/
// The internal virtual method is directly used without being declared. These methods will be implemented in the derived class, for example:
Function c1 (){}
C2.prototype = {
Fun: function () {this. fn ();} // The fn method is not defined.
}
Function c2 (){}
C1.prototype = (new c2 (). extend ({
Fn: function () {var x = 1 ;}
});
// This. initialize. apply (this, arguments); this statement transmits the parameters of the created object to the initialize method.
/***
* In javascript, you can also use the try-catch-finally statement to capture exceptions or error messages.
* E in the parentheses of catch (e) is required. e is an object named error.
* E = new Error (message) to create this object. The exception description is used as an attribute message of the error object,
*/
// This Code demonstrates the exception throw
Function s (a, B ){
Try {
If (B = 0)
Throw new Error ("the divisor cannot be zero !........ ");
Else
Alert (a/B)
} Catch (e ){
Document. write (e. message); // get the real parameter in Error through message
}
}
Onlaod = s (1, 0 );

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.