"JavaScript advanced Programming" notes-----Chapter II

Source: Internet
Author: User

Fifth Chapter

9. Function functions

1) There are two special objects inside the function:

(1) Arguments (mainly used to save function parameters, there is an attribute callee, which is a pointer to the function that owns the arguments object), but in the strict mode of the function, Arguments.callee error, you can use the function expression to complete

Eg:var FAC = (function f (num) {

Return num*f (num-1);

});

(2) This

2) Attributes: Length (number of arguments) and prototype (methods for saving instances, such as: ToString () and valueof ())

3) apply () receives 2 parameters: the scope of the run function (this or other) and the parameter array (arguments,[,,,]), in strict mode, this does not transition to window, to explicitly add the function to an object, otherwise it will point to underfined.

Call () receives multiple parameters: the first parameter runs the function's scope (this or the other) and the remainder is the arguments passed in, separated by

The above two attributes are used to extend the scope in which the function is run:

Like what:

Window.color = "Red";

var o = {color: "Blue"};

function f () {

alert (This.color);

}

f ();

F.call (this);//red

F.call (window);//red

F.call (o);//blue

4) Bind () creates an instance of the function, for example: F.bind (o);//blue

5) Special Reference type: Boolean, number, String-------------------------------àobject

(1) All objects in the Boolean return true, although the value of false is given to it, for example: var s = new Boolean (false);

(2) Number:

(1st) The ToFixed () method returns a string of numeric values according to the specified decimal number:

var num = 10;

Num.tofixed (2);//10.00

(2nd) toexponential () "Num. toexponential ();//1.0e+1" and toprecision ()

Num. toprecision (1)//1e+1

Num. toprecision (2)//10

Num. toprecision (3)//10.0

(3) String:

(1st) charAt (1) character position of that character, STRINGVALUE[1]ÀIE8 above available

(2nd) The character encoding of the charCodeAt (1) Character position

(3rd) concat (); add a string; for example: S.concat ("Das");

(4th) slice () [Eg:slice (2,7), 2 is the start position, 7 is the end position]

(5th) substr () [Eg:sunstr (2,7), 2 is the starting position, 7 is the number of characters]

(6th) substring () [Eg:substring (2,7), 2 is the start position, 7 is the end position]

(7th) Trim (); Remove the blank space before and after the string

(8th) search (); Find the index that returns the first match, no return-1

Eg:var Text = "cat, Bat";

var pos = Text.search (/at/);//1

(9th) replace (); The first argument can be a string or a regular expression, the second is the content of the substitution, which can be either a parameter or a function;

$n: Text. Replace (/.at/g, Word ($));//Word (CAT), Word (BAT)

(10th) split (), delimiter, can be one parameter, can also two parameters "second specified array size"

(11th) Localcompare (), comparing strings, before (-1), before (1), equal to (0)

(12th) The fromCharCode () of string is opposite to charCodeAt ()

Eg:string. fromCharCode (104,101);//he

(13th) Golbal: Code:

    1. encodeURI () [Encode spaces]

Encodeuricomonpent () [Encode non-standard characters {:,/,? , #} to encode]

    1. eval ();

Eg:eval ("function f () {Execute code}");

(14th) Math method: Max (), Min (), SQRT2, Sqrt1_2, Ceil () [Round up], floor () [round down], round () [Standard rounding], random () [random], abs[Absolute]

Sixth chapter

1, Data properties: (Do not use IE8)

Configurable: Default true, whether to modify the value of the property by deleting

Enumerable: Default True, whether the property is returned through a for-in loop

Writable: Default True, can I modify the value of a property

Value: The data value of the property, default underfined

To modify the default attributes of a property, you need to use Object.defineproperty ()

Eg:var person = {};

Object.defineproperty ({person, "name", {writable:false,value: "CCL"}) "ie9+, Fixfox 4+, Safari 5+, Opera 12+, Chrome"

2. Accessor properties:

Configurable: Default true, whether to modify the value of the property by deleting

Enumerable: Default True, whether the property is returned through a for-in loop

Get: Read the function value called by the property, default underfined

Set: The function value written to the property call, default underfined

To modify the default attributes of a property, you need to use Object.defineproperty (), in strict mode, get and set need to be used together, otherwise it will throw an error, in strict mode, only specify set, do not specify get will throw underfined.

Old methods for accessing containers: _definegetter_ (), _definesetter_ ()

Object.defineproperty () The second parameter can also pass in multiple properties and define properties for each property individually.

Eg:Object.defineProperty ({person,{_year: {writable:true,value: "2001"},name: {writable:false,value: "CCL"}}, set: function (NewValue) {}})

3, prototype mode (prototype): Eg:person.prototype.name= "";

1) Person.prototype.isPrototypeOf (Person1) can be used to test whether person1 points to Person.prototype

2) in ECMAScript5, Object.getprototype (person1) ==person.prototype, use Object.getprototype (Person1) to take the value. Name

3) Delete the properties of the instance: delete Person1.name

4) Detect if a property exists in the prototype, Person1.hasownprototype ("name");

5) detect if an attribute exists in the instance or in the prototype: "Name" in Person1

6) Get all instance properties that can be enumerated on the object: Object.keys (Person.prototype) or Object.keys (Person1)

7) The primitive model omits the constructor to pass the initialization parameter this part, in the silent person situation, all will obtain the same value, solves the ground, may combine constructs the function to change the pattern and the prototype pattern. All default prototypes are object instances

8) prototype inheritance can use Object.create () or object ();

Eg:var P1 = object.create (Person1); P1.name= "XX";

9) Parasitic combined inheritance: Inheritprototype (Subtype,supertype)

Seventh Chapter

1, function is the keyword, name is the name of the pointer to the function (only for Firefox, Safari, Chrom, Opera), function is not followed by an identifier, this time the creation of the functions called anonymous functions (lambda function), The Name property of the anonymous function is an empty string

Functionname.name

2. The difference between a function declaration and a function expression:

function declaration: Functions fn () {}

function expression: var SFN = function () {}

3. Closure: Create another function inside a function, the closure can only get the last value that contains any variable in the function

4, this under the closure if you want to point to a function, you should first declare the var = this, in the declaration value to call

5. Block-level scope (private scope):

(function () {

Block-level scopes

})();

6. Private variable: The variable degree defined in the function can be considered private

Privileged methods: Public methods that have access to private variables and private functions

7. Module Mode:

1) Singleton: An object with only one instance

Eg:var Singleton = {

Name: "Value",

Method:function () {

Here is the code for the method

}

}

Schemas are enhanced by adding private variables and privileged methods to the singleton

Eg:var singleton = function () {

var p = 10;

function pfunction () {return false;}

Privileges, common methods, and properties

return{

Publicproperty:true,

Publicmethod:function () {

p++;

return pfunction ();

}

}

}

2) inside the anonymous function, private variables and functions are defined, in which the object literal "interface of a single instance" is used as the return value of the function, in which case some initialization of the singleton is required, and the need to maintain its private variables:

Eg:var appliaction = function () {

private variables and functions

var components = new Array ();

Initialization

Components.push (New basecomponents ());

Public

return{

Getcomponents:function () {return components.length;},

Regiestcomponents:function (component) {

if (typeof component = = = "Object") {

Components.push (component);

}

}

}

}

3) Enhanced Module mode

var singleton = function () {

var p = 10;

function pfunction () {return false;}

Creating objects

var object = new Customtype ();

Privileges, common methods, and properties

Object.publicproperty = true;

Publicmethod:function () {

p++;

return pfunction ();

}

Return object,

}

Eighth Chapter

1, global variables cannot be deleted by the delete operator, directly on the Window object defined properties can be, because the version of Ie<9 encountered a delete will throw an error

2. Window Position:

1) Use of IE, Safari, Opera, Chrome: Screenleft and Screentop

2) Firefox, Safari, chrome use: Screenx and Screeny

3) Mobile use: MoveTo (,) and Moveby (,)

Window size

1) ie9+, Safari, Opera, Chrome, Firefox provide: Innerwidth, innerheight "minus the border width", outerwidth, Outerheight. ie9+, Safari, Firefox outerwidth, Outerheight return the size of the browser itself window, opera returns the size of the Page view container, Chrome innerwidth, Innerheight, Outerwidth, Outerheight returns the same value

2) in IE, Safari, Opera, Chrome, Firefox, Document.documentElement.clientWidth and Document.documentElement.clientHeigth Save the page viewport information, in IE6, the above standard mode is effective, in promiscuous mode, the use of Document.bod Y.clientwidth and document. Body.clientheigth, chrome in promiscuous mode, the above four kinds can be.

3) Resizeto (A, B) adjusted to A*b

Resizeby (c,d) adjustment to A+c,b+d

3, window.open () open a specific URL or information browser window. Inside can pass four parameters, to load the URL, window target (can be a target frame name or _self, _blank, _parent, _top), an attribute string (do not open a new window, ignore this, open, you can set Fullscreen[yes,no , IE], heigth, width, left, rigth, scrollbar ...), indicates whether the new page supersedes the Boolean value of the currently loaded page of the Li Lan history

var xxx=window.open (...);

Xxx.opener = = window

Window.close (); ======xxx.closed

Browser built-in masking program blocks pop-ups, window.open () may be null

4. Timeout call

var time = setTimeout (code string or function, 1000);

Cleartimeout (time);

Intermittent invocation:

var time = setinterval (code string or function, 1000);

Clearinterval (time);

5. System dialog box: Alert (), confirm (), prompt ()

6. Location is both the property of the object of window and the object of document

7. History object: History.go (-1)-Back, History.go (1)-Forward, History.go (2), History.go ("xxx.com")

You can also use back () and forward () to replace

Nineth Chapter

1, ability Monitoring: the ability to identify the browser

if (object.prototypeinquestion) {

}

Early IE5 using Document.all[id], later using document.getElementById (ID).

2. Quirks detection: Identify the special behavior of the browser and see what bugs exist

var Hasdontenumquirk = function () {

var o = (Tostring:function () {});

For (Var prop in O) {

if (prop = = "ToString") {

return false;

}

}

return true;

}();

Detecting rendering engines and browsers

var ua = navigator.useragent;

if (Window.opera) {

Engine.ver = Window.opera.version ();

Engine.opera = parsefloat (engine.ver);

} else if (/applewebkit\/(\s+)/.test (UA)) {

Engine.ver = regexp["$"];

Engine.webkit = parsefloat (engine.ver);

Are you sure it's Chrome or Safari?

if (/chrome\/(\s+)/.test (UA)) {

Browser.ver = regexp["$"];

Browser.chrome = parsefloat (browser.ver);

} else if (/version\/(s+)/test (UA)) {

Browser.ver = regexp["$"];

Borwser.safari = parsefloat (browser.ver);

} else {

Approximate definite version number

var safariversion = 1;

if (Engine.webkit < 100) {

Safariversion = 1;

} else if (Engine.webkit < 312) {

Safariversoin = 1.2;

} else if (Engine.webkit < 412) {

Safariversion = 1.3;

} else {

Safariversion = 2;

}

Browser.safari = Browser.ver = safariversion;

}

} else if (/khtml\/(\s+)/.test (UA) | |/konqueror\/([^;] +)/.test (UA)) {

Engine.ver = regexp["$"];

engine.khtml = parsefloat (engine.ver);

} else if (/RV: ([^\)]+) \) gecko\/\d{8}/.test (UA)) {

Engine.ver = regexp["$"];

Engine.gecko = parsefloat (engine.ver);

OK, not Firefox.

if (/firefox\/(\s+)/.test (UA)) {

Browser.ver = regexp["$"];

Browser.firefox = parsefloat (browser.ver);

}

} else if (/msie ([^;] +)/.test (UA)) {

Engine.ver = Browser.ver = regexp["$"];

engine.ie = browser.ie = parsefloat (engine.ver);

}

for browser

if (Client.engine.webkit) {

if (client.browser.chrome) {

}else if (Client.browser.safari) {

}

} else if (Client.engine.gecko) {

if (Client.browser.firefox) {

}else{

    

}

}

Detection engine, browser, platform, operating system

var client = function () {

Rendering engine

var engine = {

ie:0,

gecko:0,

webkit:0,

khtml:0,

opera:0,

The specific version number

Ver:null

};

var browser = {

Browser

ie:0,

firefox:0,

konq:0,

opera:0,

chrome:0,

safari:0,

The specific version

Ver:null

};

var system = {

Win:false,

Mac:false,

Xll:false

};

Detect rendering engines, platforms, and devices again

return {

Engine:engine,

Browser:browser,

System:system

};

}();

Navigator.platform values Win32, Win64, MACPPC, Macintel, X11, Linux i686

var p = navigator.platform;

System.win = P.indexof ("win") = = 0;

Systemp.mac = P.indexof ("mac") = = 0;

System.xll = (P.indexof ("XLL")) = = 1 | | (P.indexof ("Linux") = = 0);

"JavaScript advanced Programming" notes-----Chapter II

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.