Self-taught JS

Source: Internet
Author: User
Tags modulus

Through the MU class network self-taught JS, knocked a lot of code, as if there is no egg, attached code, and continue!

Property Getter Setter Method
var man = {name: ' Wsy ',
Weibo: ' @wsy ',
Get Age () {return new Date (). getFullYear ()-1996;},
Set Age (Val) {console.log (' age can\ ' t is set to ' +val);
}

var man = {
Weibo: ' @wsy ',
$age: null,
Get Age () {
if (this. $age = = undefined) {
return new Date (). getFullYear ()-1996;
}else{
return this. $age;
}
},
Set Age (Val) {
val = +val;
if (!isnan (val) &&val >0 && val < 150) {
this. $age = +val;
}else{
throw new Error (' Incorrect val = ' +val);
}
}
}
Attribute tags and actions
function foo () {}
Object.defineproperty (Foo.prototype, ' Z ', {get:function () {return 1;}});
var obj = new Foo ();

Object.defineproperty (obj, ' z ', {value:100,configurable:true});


var o = {};
Object.defineproperty (o, ' x ', {value:1});
var obj = object.create (o);

Object.defineproperty (obj, ' x ', {writable:true, configurable:true, value:100});

Object.getownpropertydescriptor ({pro:true}, ' Pro ');
Object.getownpropertydescriptor ({pro:true}, ' a ');

var person = {};
Object.defineproperty (person, ' name ', {
Configurable:false,
Writable:false,
Enumerable:false,
Value: "Wsy"
});
Object.defineproperty (person, ' type ', {
Configurable:true,
Writable:true,
Enumerable:false,
Value: "Object"
});

Object.defineproperties (person, {
Title: {value: ' Fe ', enumerable:true},
Corp: {value: ' BABA ', enumerable:true},
Salary: {value:50000, enumerable:true, writable:true}
});
Object.getownpropertydescriptor (person, ' salary ');
Object.getownpropertydescriptor (person, ' Corp ');

Object.defineproperties (person, {
Title: {value: ' Fe ', enumerable:true},
Corp: {value: ' BABA ', enumerable:true},
Salary: {value:50000, enumerable:true, writable:true},
Luck: {
Get:function () {
return Math.random () > 0.5? ' Good ': ' bad ';
}
},
Promote: {
Set:function (level) {
This.salary *= 1 + level * 0.1;
}
}
});

Object tags [[extensible]]
var obj = {x:1, y:2};
Object.isextensible (obj);
Object.preventextensions (obj);
Object.isextensible (obj);
Obj.z = 1;
Obj.z;
Object.getownpropertydescriptor (obj, ' x ');

Object.seal (obj);
Object.getownpropertydescriptor (obj, ' x ');
object.issealed (obj);

Object.freeze (obj);
Object.getownpropertydescriptor (obj, ' x ');
Object.isfrozen (obj);

Serialization of
var obj = {x:1, y:true, z: [N/A], nullval:null};
Json.stringify (obj);

obj = {val:undefined, A:nan, b:infinity, C:new Date ()};
Json.stringify (obj);

obj = Json.parse (' {"X": 1} ');
obj.x;

Serialization-Custom
var obj = {
X:1,
Y:2,
O: {
O1:1,
O2:2,
Tojson:function () {
return this.o1 + This.o2;
}
}
};
Json.stringify (obj);

Other object methods
var obj = {x:1, y:2};
Obj.tostring ();
obj.tostring = function () {return this.x + this.y};
"Result" + obj;

+obj;

obj.valueof = function () {return this.x + this.y + 100};
+obj;

"Result" + obj;

Array manipulation
var arr = [];
Arr[0] = 1;
ARR[1] = 2;
Arr.push (3);
Arr
ARR[2] = undefined;
2 in Arr;//true
Delete Arr[2];
2 in Arr;//false

Traversing a two-dimensional array
var arr = [[1,2],[3,4],[5,6]];
var i = 0;
var row;
for (; i<arr.length;i++) {
row = Arr[i];
Console.log (' Row ' +i);
for (j = 0; j<row.length;j++) {
Console.log (Row[j]);
}
}

Array method
Join
var arr = [n/a];
Arr.join ();
Arr.join ("_");

function repeatstring (str, n) {
return new Array (n+1). Join (str);
}
Repeatstring ("A", 3);
Repeatstring ("Hi", 5);
Reverse
var arr = [n/a];
Arr.reverse ();
Arr
Sort
var arr = ["A", "D", "B", "C"];
Arr.sort ();

arr = [13,24,3,44,5];
Arr.sort ();
Arr

Arr.sort (function (A, b) {
return a-B;
})

arr = [{age:25},{age:39},{age:99}];
Arr.sort (function (A, b) {
return a.age-b.age;
})
Arr.foreach (function (item) {
Console.log (' age ', item.age);
})
Concat
var arr = [n/a];
Arr.concat (4,5);
Arr

Arr.concat ([10,11],13);

Arr.concat ([1,[2,3]]);
Slice
var arr = [1,2,3,4,5];
Arr.slice (1,3);
Arr.slice (1);
Arr.slice (1,-1);
Arr.slice (-4,-3);
Splice
var arr = [1,2,3,4,5];
Arr.splice (2);
Arr

arr = [1,2,3,4,5];
Arr.splice (2,2);
Arr

arr = [1,2,3,4,5];
Arr.splice (A, ' a ', ' B ');
Arr
Foreach
var arr = [1,2,6,4,5];
Arr.foreach (function (x, index, a) {
Console.log (x + ' | ' + index + ' | ' + (a = = = arr));
});
Map
var arr = [n/a];
Arr.map (function (x) {
return x + 10;
})
Arr
Filter
var arr = [1,2,3,4,5,6,7,8,9,10];
Arr.filter (function (x,index) {
return index%3 ===0| | X >=8;
});
Arr
Every&some
var arr = [1,2,3,4,5];
Arr.every (function (x) {
return x <10;
});
Arr.every (function (x) {
return x <3;
});

var arr = [1,2,3,4,5];
Arr.some (function (x) {
return x = = 10;
});
Arr.some (function (x) {
return x = = 3;
});
Reduce&reduceright
var arr = [n/a];
var sum = arr.reduce (function (x, y) {
return x + y;
},0);
Arr

arr = [3,9,6];
var max = Arr.reduce (function (x, y) {
Console.log (x + "|" +y);
return x > Y? x:y;
});
Max

arr = [3,9,6];
var max = Arr.reduceright (function (x, y) {
Console.log (x + "|" +y);
return x > Y? x:y;
});
Max
Indexof&lastindexof
var arr = [1,2,3,2,1];
Arr.indexof (2);
Arr.indexof (99);
Arr.indexof (a);
Arr.indexof (2,-1);
Arr.lastindexof (2);
Arr.lastindexof (2,2);
Arr.lastindexof (2,-3);
Array.isarray
Array.isarray ([]);
[] instanceof Array;
({}). tostring.apply ([]) = = = ' [Object Array] ';

Declaration of variables & functions predecessor
var num = Add (+);
Console.log (num);

function Add (A, b) {
A = + A;
b = + B;
if (IsNaN (a) | | IsNaN (b)) {
Return
}
return a + B;
}


var num = Add (+);
Console.log (num);

var add = function (A, b) {
A = + A;
b = + B;
if (IsNaN (a) | | IsNaN (b)) {
Return
}
return a + B;
}

The formula for naming the function table (NFE)
var func = function Nfe () {};
Alert (func = = = NFE);//uncaught REFERENCEERROR:NFE is not defined at <anonymous>:2:16

Function constructor
var func = new Function (' A ', ' B ', ' Console.log (A + b); ');
Func (1, 2);

var func = Function (' A ', ' B ', ' Console.log (A + b); ');
Func (1, 2);

Function (' var localval = ' local '; Console.log (localval); ') ();
Console.log (typeof Localval);

var globalval = ' global ';
(function () {
var localval = ' local ';
Function (' Console.log (typeof Localval, typeof Globalval); ();
})();


Global this (browser)
Console.log (this.document = = = document);

Console.log (This = = = window);

THIS.A = 37;
Console.log (WINDOW.A);
?
This (browser) of the general function
Function F1 () {
return this;
}
F1 () = = = window;

function F2 () {
"Use strict";
return this;
}
F2 () = = = undefined;

This of a function as an object method
var o = {
Prop:37,
F:function () {
return this.prop;
}
};
Console.log (O.F ());

var o = {prop:36};
function Independent () {
return this.prop;
}
O.F = Independent;
Console.log (O.F ());

This on the object prototype chain
var o = {f:function () {return this.a + this.b;}};
var p = object.create (o);
P.A = 1;
P.B = 4;
Console.log (P.F ());

Get/set method and this
function modulus () {
Return math.sqrt (this.re * this.re + this.im * this.im);
}
var o = {
Re:1,
Im:-1,
Get phase () {
Return math.atan2 (this.im, this.re);
}
};
Object.defineproperty (o, ' modulus ', {
Get:modulus, Enumerable:true, configurable:true
});
Console.log (O.phase, O.modulus);

This in the constructor
function MyClass () {
THIS.A = 37;
}
var o = new MyClass ();
Console.log (O.A);

function C2 () {
THIS.A = 37;
return {a:38};
}
o = new C2 ();
Console.log (O.A);

Call/apply method and this
function Add (c, D) {
Return THIS.A + this.b + C + D;
}
var o = {a:1, b:3};
Add.call (O, 4, 5);
Add.apply (o, [10, 20]);

function Bar () {
Console.log (Object.prototype.toString.call (this));
}
Bar.call (7);

Bind method with this
function f () {
return THIS.A;
}
var g = F.bind ({A: "Test"});
Console.log (g ());

var o = {a:37, f:f, g:g};
Console.log (O.F (), O.G ());

Self-taught JS

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.