객체 유형
배열 유형
재주문 방법 : 비교
오름차순 순서 :
함수 비교 (value1, value2) {if (value1 <value2) {return -1; } if (value1> value2) {return 1; } else {return 0; }} var 값 = [0,1,5,10,15]; values.sort (비교); Console.log (값); // [0,1,5,10,15]하강 순서 :
함수 비교 (value1, value2) {if (value1 <value2) {return 1; } if (value1> value2) {return -1; } else {return 0; }}일부분:
슬라이스 (시작, 끝); Slice () 메소드는 매개 변수의 지정된 위치에서 현재 배열의 끝까지 시작하는 모든 항목을 반환합니다. 두 개의 매개 변수가있는 경우 메소드는 시작과 끝 위치 사이의 항목을 반환하지만 끝 위치에 항목을 포함하지 않습니다.
var colors = [ "Red", "Green", "Blue", "Yellow", "Purple"]; var colors2 = colors.slice (1); var colors3 = colors.slice (1,4); console.log (colors2); // 녹색, 파란색, 노란색, purpleconsole.log (colors3); // 녹색, 파란색, 노란색
접착:
Splice ()에는 삭제, 삽입 및 교체 기능이 있습니다
삭제:
첫 번째 항목의 위치는 삭제 될 두 개의 매개 변수가 필요합니다.
var colors = [ "빨간색", "녹색", "파란색"]; var removed = colors.splice (0,1); console.log (colors); // green, blueconsole.log (제거); // 빨간색
끼워 넣다:
세 가지 매개 변수가 필요합니다 : 시작 위치, 0 (삭제할 항목 수) 및 삽입 할 항목
var colors = [ "빨간색", "녹색", "파란색"]; var removed = colors.splice (1,0, "옐로우", "오렌지"); console.log (colors); // [ "빨간색", "노란색", "오렌지", "녹색", "파란색"] console.log (제거); // 비어있는 것으로 돌아갑니다
바꾸다:
시작 위치, 삭제할 항목 수 및 삽입 할 항목의 세 가지 매개 변수가 필요합니다.
var colors = [ "빨간색", "녹색", "파란색"]; var removed = colors.splice (1,1, "옐로우", "오렌지"); console.log (colors); // [ "빨간색", "옐로우", "오렌지", "파란색"] console.log (제거); // ["녹색"]
날짜 유형
regexp 유형
var pattern1 = /[bc] /i; var pattern2 = new regexp ( "[bc] at", "i");
Pattern1 및 Pattern2는 완전히 동등한 두 개의 정규 표현입니다. Regexp 생성자로 전달 된 두 파라미터는 문자열입니다 (Regex Literals는 Regexp 생성자에게 전달 될 수 없음). REGEXP 생성자의 패턴 인수는 문자열이므로 경우에 따라 문자열이 이중 탈출됩니다.
var pattern1 =/[bc]/i; var pattern2 = new regexp ( "// [bc //] at", "i");
regexp 인스턴스 메소드
exec
exec는 매개 변수, 즉 패턴을 적용하는 문자열을 수신하고 첫 번째 일치하는 정보를 포함하는 배열을 반환합니다.
var text = "cat, bat, sat, fat"; var pattern1 = /.at/;var matches = pattern1.exec (text); console.log (일치); // ["고양이"]
성냥
일치는 문자열이 일치하는 정규 표현 규칙을 실행하는 방법이며, 매개 변수는 정규식입니다.
var text = "cat, bat, sat, fat"; var pattern1 = /.at/;var matches2 = text.match (Pattern1); console.log (matches2); // ["고양이"]
시험
test ()는 문자열 매개 변수를 수신합니다
var text = "000-00-0000"; var pattern = // d {3}-/d {2}-/d {4}/; if (pattern.test (text)) {console.log ( "패턴이 일치했습니다"); // 패턴이 일치했습니다}기능 유형
기능 내부 속성 기능
인수를 배열로 변환합니다
(함수 () {var slice = array.prototype.slice, aarguments = slice.apply (arguments); console.log (aarguments);}) (10, 20, 30); arguments.callee이 속성은이 인수 객체를 소유 한 함수에 대한 포인터입니다. 함수가 엄격한 모드로 실행될 때 인수에 액세스하면 Callee가 오류가 발생합니다.
기능 속성 및 방법
길이
길이 속성은 함수가 수신하려는 지명 된 매개 변수의 수를 나타냅니다.
function sayname (name) {alert (name);} function sum (num1, num2) {return num1 + num2;} function sayhi () {alert ( "hi");} console.log (sayname.length); //1console.log(sum.length); //2console.log(sayhi.length); // 0원기
전화, 신청하십시오
함수 sum (num1, num2) {return num1 + num2;} 함수 callum1 (num1, num2) {return sum.apply (this, arguments);} 함수 callum2 (num1, num2) {return sum.apply (this, [num1, num2]); } console.log (Callsum1 (10,10)); // 20console.log (Callsum2 (10,10)); //20window.color = "red"; var o = {color : "blue"}; function saycolor () {console.log (this.color);} saycolor (); // redsaycolor.call (this); // redsaycolor.call (창); // redsaycolor.call (o); // 파란색기본 포장 유형
var value = "25"; var number = number (value); console.log (typeof number); console.log (숫자 인스턴스 번호); // falsevar obj = 새 번호 (value); console.log (typeof obj); console.log (obj instanceof numb); // true
부울 유형
var falseObject = new boolean (false); var result = falseObject && true; // true // 부울 표현식의 모든 객체는 true로 변환되므로 FalseObject 객체는 trueconsole.log (result)를 나타냅니다. // truevar falsevalue = false; result = falseValue && true; console.log (result); //falseconsole.log(typeof falseObject); //objectConsole.log(typeof falseValue); // booleanConsole.log (falseObject instance of boolean); //trueconsole.log(falsevalue boolean의 인스턴스); // 거짓
숫자 유형
var numberObject = 새 번호 (10); var numberValue = 10; console.log (typeof numberObject); // ObjectConsole.log (typoef 숫자 값); // numberConsole.log (numberObject instanceOf 번호); // trueconsole.log (숫자 value instanceof number); // 거짓
문자열 유형
문자 방법
charat () charcodeat ()
charat () 메소드는 주어진 위치에서 단일 문자열로 문자열을 반환합니다.
charcodeat ()는 문자 인코딩을 반환합니다.
var stringValue = "Hello World"; console.log (StringValue.charat (1)); // econsole.log (StringValue.charcodeat (1)); // 101
문자열 작동 방법
concat ()
concat ()는 하나 이상의 문자열을 스플 라이스하는 데 사용됩니다.
var stringValue = "hello"; var result = stringValue.concat ( "World"); console.log (결과); // Hello WorldConsole.Log (StringValue); // 안녕하세요
슬라이스 (시작, 끝)
끝은 문자열이 끝나는 곳을 의미합니다.
전달 된 숫자로 전달되면 Slice () 메소드는 통과 된 전달 된 값을 문자열 길이에 추가합니다.
var str = "Hello Happy World!"; console.log (str.slice (6)); // Happy World! console.log (str.slice (6,11)); // happyConsole.log (str.slice (-3)); // ld! console.log (str.slice (3, -4)); // LO HAPPY WO
서브 스트링 (시작, 끝)
음수로 전달되면 substring ()은 모든 문자 매개 변수를 0으로 변환합니다.
var str = "Hello Happy World!"; console.log (str.substring (6)); // Happy World! console.log (str.substring (6,11)); // Happy World.log (str.substring (-3)); // Hello Happy World! console.log (str.substring (3, -4)); // 헬
기판 (시작, 길이)
전달 된 숫자가 음수 인 경우 substr () 메소드는 음의 첫 번째 매개 변수를 문자열의 길이에 추가하고 음의 두 번째 매개 변수를 0으로 변환합니다.
var str = "hello world!"; console.log (str.substr (3)); // lo world! console.log (str.substr (3, 7)); // lo worldConsole.log (str.substr (-3)); // ld! console.log (str.substr (3, -3)); // 비어있는 문자열
문자열 위치 방법
indexof () lastIndexof () var stringValue = "hello world"; console.log (StringValue.indexof ( "o")); // 4console.log (StringValue.lastIndexof ( "O")); // 7
두 방법 모두 문자열에서 검색을 시작할 위치를 나타내는 선택적 두 번째 매개 변수를받을 수 있습니다.
var stringValue = "Hello World"; console.log (StringValue.indexof ( "O", 6)); // 7console.log (StringValue.lastIndexof ( "O", 6)); // 4
문자열 패턴 매칭 방법
성냥()
var text = "cat, bat, sat, fat"; var pattern = /.at/;var matches = text.match (Pattern); console.log (matches.index); //0console.log(matches=]); // catconsole.log (Pattern.lastIndex); // 0
찾다()
var text = "cat, bat, sat, fat"; var pos = text.search (/at/); console.log (pos); // 1
바꾸다()
var text = "cat, bat, sat, fat"; var result = text.replace ( "at", "ond"); console.log (result); // cond, bat, sat, fatvar result = text.replace (/at/g, "ond"); console.log (결과); // Cond, Bond, Sond, Fond
글로벌 대상
URI 인코딩 방법
글로벌 오브젝트의 encodeuri () 및 encodeUricomponent () 메소드는 브라우저로 전송하기 위해 URI (균일 리소스 식별자)를 인코딩 할 수 있습니다.
var url = "http://www.baidu.com/"; console.log (encodeuri (url)); console.log (encodeuricomponent (url)); encodeuri () 및 encodeUricomponent () 메소드 객체는 decodeuri () 및 decodeUricomponent ()입니다.
수학 대상
랜덤 () 메소드
Math.random () 메소드는 0과 1 사이의 임의의 숫자를 반환합니다. 0과 1을 포함하지 않습니다.이 방법은 유명한 따옴표 및 뉴스 이벤트를 무작위로 표시하는 데 사용될 수 있기 때문에 일부 사이트에서는 매우 실용적입니다. 다음 공식을 적용하면 Math.random ()을 사용하여 다양한 정수에서 값을 무작위로 선택할 수 있습니다.
value = math.floor (math.random () *가능한 총 값 + 첫 번째 가능한 값)
예를 들어 : 1에서 10 사이의 값을 선택하려면 다음과 같은 코드를 쓸 수 있습니다.
var num = math.floor (math.random ()*10 + 1); 함수 selectfrom (lowervalue, toppervalue) {var choice = toppervalue -lowervalue + 1; Return Math.floor (Math.random ()*Choice+LowerValue);} var num = selectfrom (2,10); console.log (num); var colors = [ "빨간색", "녹색", "파란색", "옐로우", "검은", "보라색", "brown"], "selectfrom (0, colors.length-1)];