Hướng dẫn type conversion javascript - loại javascript chuyển đổi

Number[] chuyển thành một số, String[] chuyển thành một chuỗi, Boolean[] chuyển thành một Boolean.

Các kiểu dữ liệu trong JavaScript[JavaScript Data Types]

Trong JavaScript có 5 kiểu dữ liệu khác nhau có thể chứa các giá trị:

  • 
    
    
    
    

    The JavaScript constructor Property

    The constructor property returns the constructor function for a variable or an object.

    document.getElementById["demo"].innerHTML = "john".constructor + "
    " + [3.14].constructor + "
    " + false.constructor + "
    " + [1,2,3,4].constructor + "
    " + {name:'john', age:34}.constructor + "
    " + new Date[].constructor + "
    " + function [] {}.constructor;
    0
  • 
    
    
    
    

    The JavaScript constructor Property

    The constructor property returns the constructor function for a variable or an object.

    document.getElementById["demo"].innerHTML = "john".constructor + "
    " + [3.14].constructor + "
    " + false.constructor + "
    " + [1,2,3,4].constructor + "
    " + {name:'john', age:34}.constructor + "
    " + new Date[].constructor + "
    " + function [] {}.constructor;
    1
  • 
    
    
    
    

    The JavaScript constructor Property

    The constructor property returns the constructor function for a variable or an object.

    document.getElementById["demo"].innerHTML = "john".constructor + "
    " + [3.14].constructor + "
    " + false.constructor + "
    " + [1,2,3,4].constructor + "
    " + {name:'john', age:34}.constructor + "
    " + new Date[].constructor + "
    " + function [] {}.constructor;
    2
  • 
    
    
    
    

    The JavaScript constructor Property

    The constructor property returns the constructor function for a variable or an object.

    document.getElementById["demo"].innerHTML = "john".constructor + "
    " + [3.14].constructor + "
    " + false.constructor + "
    " + [1,2,3,4].constructor + "
    " + {name:'john', age:34}.constructor + "
    " + new Date[].constructor + "
    " + function [] {}.constructor;
    3
  • 
    
    
    
    

    The JavaScript constructor Property

    The constructor property returns the constructor function for a variable or an object.

    document.getElementById["demo"].innerHTML = "john".constructor + "
    " + [3.14].constructor + "
    " + false.constructor + "
    " + [1,2,3,4].constructor + "
    " + {name:'john', age:34}.constructor + "
    " + new Date[].constructor + "
    " + function [] {}.constructor;
    4

Có 6 kiểu đối tượng:

  • 
    
    
    
    

    The JavaScript constructor Property

    The constructor property returns the constructor function for a variable or an object.

    document.getElementById["demo"].innerHTML = "john".constructor + "
    " + [3.14].constructor + "
    " + false.constructor + "
    " + [1,2,3,4].constructor + "
    " + {name:'john', age:34}.constructor + "
    " + new Date[].constructor + "
    " + function [] {}.constructor;
    5
  • 
    
    
    
    

    The JavaScript constructor Property

    The constructor property returns the constructor function for a variable or an object.

    document.getElementById["demo"].innerHTML = "john".constructor + "
    " + [3.14].constructor + "
    " + false.constructor + "
    " + [1,2,3,4].constructor + "
    " + {name:'john', age:34}.constructor + "
    " + new Date[].constructor + "
    " + function [] {}.constructor;
    6
  • 
    
    
    
    

    The JavaScript constructor Property

    The constructor property returns the constructor function for a variable or an object.

    document.getElementById["demo"].innerHTML = "john".constructor + "
    " + [3.14].constructor + "
    " + false.constructor + "
    " + [1,2,3,4].constructor + "
    " + {name:'john', age:34}.constructor + "
    " + new Date[].constructor + "
    " + function [] {}.constructor;
    7
  • 
    
    
    
    

    The JavaScript constructor Property

    The constructor property returns the constructor function for a variable or an object.

    document.getElementById["demo"].innerHTML = "john".constructor + "
    " + [3.14].constructor + "
    " + false.constructor + "
    " + [1,2,3,4].constructor + "
    " + {name:'john', age:34}.constructor + "
    " + new Date[].constructor + "
    " + function [] {}.constructor;
    8
  • 
    
    
    
    

    The JavaScript constructor Property

    The constructor property returns the constructor function for a variable or an object.

    document.getElementById["demo"].innerHTML = "john".constructor + "
    " + [3.14].constructor + "
    " + false.constructor + "
    " + [1,2,3,4].constructor + "
    " + {name:'john', age:34}.constructor + "
    " + new Date[].constructor + "
    " + function [] {}.constructor;
    9
  • function isArray[myArray] {
      return myArray.constructor.toString[].indexOf["Array"] > -1;
    }
    0

Và hai kiểu dữ liệu không thể chứa giá trị:

  • function isArray[myArray] {
      return myArray.constructor.toString[].indexOf["Array"] > -1;
    }
    1
  • function isArray[myArray] {
      return myArray.constructor.toString[].indexOf["Array"] > -1;
    }
    2

Toán tử typeof

Bạn có thể sử dụng toán tử 

function isArray[myArray] {
  return myArray.constructor.toString[].indexOf["Array"] > -1;
}
3 để tìm kiểu dữ liệu của một biến JavaScript.
function isArray[myArray] {
  return myArray.constructor.toString[].indexOf["Array"] > -1;
}
3 để tìm kiểu dữ liệu của một biến JavaScript.

ví dụ

typeof "John"                 // Returns "string"
typeof 3.14                   // Returns "number"
typeof NaN                    // Returns "number"
typeof false                  // Returns "boolean"
typeof [1,2,3,4]              // Returns "object"
typeof {name:'John', age:34}  // Returns "object"
typeof new Date[]             // Returns "object"
typeof function [] {}         // Returns "function"
typeof myCar                  // Returns "undefined" *
typeof null                   // Returns "object"




The JavaScript typeof Operator

The typeof operator returns the type of a variable, object, function or expression.

document.getElementById["demo"].innerHTML = typeof "john" + "
" + typeof 3.14 + "
" + typeof NaN + "
" + typeof false + "
" + typeof [1,2,3,4] + "
" + typeof {name:'john', age:34} + "
" + typeof new Date[] + "
" + typeof function [] {} + "
" + typeof myCar + "
" + typeof null;


Xin hãy quan sát:

  • Kiểu dữ liệu của NaN là number
  • Kiểu dữ liệu của mảng là object
  • Kiểu dữ liệu của thời gian là object
  • Kiểu dữ liệu của null là object
  • Kiểu dữ liệu của một biến không xác định là undefined * undefined *
  • Kiểu dữ liệu của một biến chưa được gán giá trị cũng là undefined * undefined *

Tip: Bạn không thể dùng typeof để xác định nếu một đối tượng JavaScript là một mảng [hoặc một thời gian].typeof để xác định nếu một đối tượng JavaScript là một mảng [hoặc một thời gian].

Kiểu dữ liệu của typeof


Toán tử 

function isArray[myArray] {
  return myArray.constructor.toString[].indexOf["Array"] > -1;
}
3 không phải là một biến. Nó là một toán tử. Toán tử [ +-*/ ] không có bất kì kiểu dữ liệu.
function isArray[myArray] {
  return myArray.constructor.toString[].indexOf["Array"] > -1;
}
3 không phải là một biến. Nó là một toán tử. Toán tử [ +-*/ ] không có bất kì kiểu dữ liệu.

Nhưng, toán tử  

function isArray[myArray] {
  return myArray.constructor.toString[].indexOf["Array"] > -1;
}
3 luôn trả về một chuỗi [chứa kiểu của toán hạng]. 
function isArray[myArray] {
  return myArray.constructor.toString[].indexOf["Array"] > -1;
}
3 luôn trả về một chuỗi [chứa kiểu của toán hạng].

Thuộc tính constructor[The constructor Property]

Thuộc tính 

function isArray[myArray] {
  return myArray.constructor.toString[].indexOf["Array"] > -1;
}
6 trả về hàm tạo cho toàn bộ các biến JavaScript.
function isArray[myArray] {
  return myArray.constructor.toString[].indexOf["Array"] > -1;
}
6 trả về hàm tạo cho toàn bộ các biến JavaScript.

ví dụ

"John".constructor                // Returns function String[]  {[native code]}
[3.14].constructor                // Returns function Number[]  {[native code]}
false.constructor                 // Returns function Boolean[] {[native code]}
[1,2,3,4].constructor             // Returns function Array[]   {[native code]}
{name:'John',age:34}.constructor  // Returns function Object[]  {[native code]}
new Date[].constructor            // Returns function Date[]    {[native code]}
function [] {}.constructor        // Returns function Function[]{[native code]}




The JavaScript constructor Property

The constructor property returns the constructor function for a variable or an object.

document.getElementById["demo"].innerHTML = "john".constructor + "
" + [3.14].constructor + "
" + false.constructor + "
" + [1,2,3,4].constructor + "
" + {name:'john', age:34}.constructor + "
" + new Date[].constructor + "
" + function [] {}.constructor;


Xin hãy quan sát:





The JavaScript constructor Property

The constructor property returns the constructor function for a variable or an object.

document.getElementById["demo"].innerHTML = "john".constructor + "
" + [3.14].constructor + "
" + false.constructor + "
" + [1,2,3,4].constructor + "
" + {name:'john', age:34}.constructor + "
" + new Date[].constructor + "
" + function [] {}.constructor;
7 [chưa từ "Array"]:

ví dụ

function isArray[myArray] {
  return myArray.constructor.toString[].indexOf["Array"] > -1;
}



JavaScript Arrays

This "home made" isArray[] function returns true when used on an array:

var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById["demo"].innerHTML = isArray[fruits]; function isArray[myArray] { return myArray.constructor.toString[].indexOf["Array"] > -1; }


Xin hãy quan sát: hàm Array :

ví dụ

Xin hãy quan sát:


Kiểu dữ liệu của NaN là number





The JavaScript constructor Property

The constructor property returns the constructor function for a variable or an object.

document.getElementById["demo"].innerHTML = "john".constructor + "
" + [3.14].constructor + "
" + false.constructor + "
" + [1,2,3,4].constructor + "
" + {name:'john', age:34}.constructor + "
" + new Date[].constructor + "
" + function [] {}.constructor;
6 [chưa từ khóa "Date"]:

ví dụ

function isDate[myDate] {
  return myDate.constructor.toString[].indexOf["Date"] > -1;
}




JavaScript Date Object

This "home made" isDate[] function returns true when used on an date:

var myDate = new Date[]; document.getElementById["demo"].innerHTML = isDate[myDate]; function isDate[myDate] { return myDate.constructor.toString[].indexOf["Date"] > -1; }


Xin hãy quan sát: hàm Date :

ví dụ





The JavaScript typeof Operator

The typeof operator returns the type of a variable, object, function or expression.

document.getElementById["demo"].innerHTML = typeof "john" + "
" + typeof 3.14 + "
" + typeof NaN + "
" + typeof false + "
" + typeof [1,2,3,4] + "
" + typeof {name:'john', age:34} + "
" + typeof new Date[] + "
" + typeof function [] {} + "
" + typeof myCar + "
" + typeof null;
0




The JavaScript typeof Operator

The typeof operator returns the type of a variable, object, function or expression.

document.getElementById["demo"].innerHTML = typeof "john" + "
" + typeof 3.14 + "
" + typeof NaN + "
" + typeof false + "
" + typeof [1,2,3,4] + "
" + typeof {name:'john', age:34} + "
" + typeof new Date[] + "
" + typeof function [] {} + "
" + typeof myCar + "
" + typeof null;
1

Xin hãy quan sát:


Kiểu dữ liệu của NaN là number

  • Kiểu dữ liệu của mảng là object
  • Kiểu dữ liệu của thời gian là objectbởi chính JavaScript

Kiểu dữ liệu của null là object


Kiểu dữ liệu của một biến không xác định là undefined *

function isArray[myArray] {
  return myArray.constructor.toString[].indexOf["Array"] > -1;
}
9 có thể chuyển số thành chuỗi.

Kiểu dữ liệu của một biến chưa được gán giá trị cũng là undefined *

ví dụ





The JavaScript typeof Operator

The typeof operator returns the type of a variable, object, function or expression.

document.getElementById["demo"].innerHTML = typeof "john" + "
" + typeof 3.14 + "
" + typeof NaN + "
" + typeof false + "
" + typeof [1,2,3,4] + "
" + typeof {name:'john', age:34} + "
" + typeof new Date[] + "
" + typeof function [] {} + "
" + typeof myCar + "
" + typeof null;
2




The JavaScript typeof Operator

The typeof operator returns the type of a variable, object, function or expression.

document.getElementById["demo"].innerHTML = typeof "john" + "
" + typeof 3.14 + "
" + typeof NaN + "
" + typeof false + "
" + typeof [1,2,3,4] + "
" + typeof {name:'john', age:34} + "
" + typeof new Date[] + "
" + typeof function [] {} + "
" + typeof myCar + "
" + typeof null;
3


Xin hãy quan sát:




JavaScript Arrays

This "home made" isArray[] function returns true when used on an array:

var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById["demo"].innerHTML = isArray[fruits]; function isArray[myArray] { return myArray.constructor.toString[].indexOf["Array"] > -1; }
0 cũng làm y hệt.

ví dụ





The JavaScript typeof Operator

The typeof operator returns the type of a variable, object, function or expression.

document.getElementById["demo"].innerHTML = typeof "john" + "
" + typeof 3.14 + "
" + typeof NaN + "
" + typeof false + "
" + typeof [1,2,3,4] + "
" + typeof {name:'john', age:34} + "
" + typeof new Date[] + "
" + typeof function [] {} + "
" + typeof myCar + "
" + typeof null;
4




The JavaScript typeof Operator

The typeof operator returns the type of a variable, object, function or expression.

document.getElementById["demo"].innerHTML = typeof "john" + "
" + typeof 3.14 + "
" + typeof NaN + "
" + typeof false + "
" + typeof [1,2,3,4] + "
" + typeof {name:'john', age:34} + "
" + typeof new Date[] + "
" + typeof function [] {} + "
" + typeof myCar + "
" + typeof null;
5


Xin hãy quan sát:

Kiểu dữ liệu của NaN là numberKiểu dữ liệu của mảng là object
Kiểu dữ liệu của thời gian là objectKiểu dữ liệu của null là object
Kiểu dữ liệu của một biến không xác định là undefined *Kiểu dữ liệu của một biến chưa được gán giá trị cũng là undefined *
Tip: Bạn không thể dùng typeof để xác định nếu một đối tượng JavaScript là một mảng [hoặc một thời gian].Kiểu dữ liệu của typeof

Toán tử 
function isArray[myArray] {
  return myArray.constructor.toString[].indexOf["Array"] > -1;
}
3 không phải là một biến. Nó là một toán tử. Toán tử [ +-*/ ] không có bất kì kiểu dữ liệu.


Nhưng, toán tử  

function isArray[myArray] {
  return myArray.constructor.toString[].indexOf["Array"] > -1;
}
3 luôn trả về một chuỗi [chứa kiểu của toán hạng].
function isArray[myArray] {
  return myArray.constructor.toString[].indexOf["Array"] > -1;
}
9 có thể chuyển boolean thành chuỗi.





The JavaScript typeof Operator

The typeof operator returns the type of a variable, object, function or expression.

document.getElementById["demo"].innerHTML = typeof "john" + "
" + typeof 3.14 + "
" + typeof NaN + "
" + typeof false + "
" + typeof [1,2,3,4] + "
" + typeof {name:'john', age:34} + "
" + typeof new Date[] + "
" + typeof function [] {} + "
" + typeof myCar + "
" + typeof null;
6

Thuộc tính constructor[The constructor Property]


Thuộc tính 

function isArray[myArray] {
  return myArray.constructor.toString[].indexOf["Array"] > -1;
}
6 trả về hàm tạo cho toàn bộ các biến JavaScript.
function isArray[myArray] {
  return myArray.constructor.toString[].indexOf["Array"] > -1;
}
9 có thể chuyển đổi thời gian thành chuỗi.





The JavaScript typeof Operator

The typeof operator returns the type of a variable, object, function or expression.

document.getElementById["demo"].innerHTML = typeof "john" + "
" + typeof 3.14 + "
" + typeof NaN + "
" + typeof false + "
" + typeof [1,2,3,4] + "
" + typeof {name:'john', age:34} + "
" + typeof new Date[] + "
" + typeof function [] {} + "
" + typeof myCar + "
" + typeof null;
7


Bạn có thể kiểm tra thuộc tính cấu trúc để tìm ra nếu một đối tượng là một 





The JavaScript constructor Property

The constructor property returns the constructor function for a variable or an object.

document.getElementById["demo"].innerHTML = "john".constructor + "
" + [3.14].constructor + "
" + false.constructor + "
" + [1,2,3,4].constructor + "
" + {name:'john', age:34}.constructor + "
" + new Date[].constructor + "
" + function [] {}.constructor;
7 [chưa từ "Array"]:



JavaScript Arrays

This "home made" isArray[] function returns true when used on an array:

var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById["demo"].innerHTML = isArray[fruits]; function isArray[myArray] { return myArray.constructor.toString[].indexOf["Array"] > -1; }
0 cũng làm y hệt.

ví dụ





The JavaScript typeof Operator

The typeof operator returns the type of a variable, object, function or expression.

document.getElementById["demo"].innerHTML = typeof "john" + "
" + typeof 3.14 + "
" + typeof NaN + "
" + typeof false + "
" + typeof [1,2,3,4] + "
" + typeof {name:'john', age:34} + "
" + typeof new Date[] + "
" + typeof function [] {} + "
" + typeof myCar + "
" + typeof null;
8

Xin hãy quan sát:

Kiểu dữ liệu của NaN là numberKiểu dữ liệu của mảng là object
Kiểu dữ liệu của thời gian là objectKiểu dữ liệu của null là object
Kiểu dữ liệu của một biến không xác định là undefined *Kiểu dữ liệu của một biến chưa được gán giá trị cũng là undefined *
Tip: Bạn không thể dùng typeof để xác định nếu một đối tượng JavaScript là một mảng [hoặc một thời gian].Kiểu dữ liệu của typeof
Toán tử 
function isArray[myArray] {
  return myArray.constructor.toString[].indexOf["Array"] > -1;
}
3 không phải là một biến. Nó là một toán tử. Toán tử [ +-*/ ] không có bất kì kiểu dữ liệu.
Nhưng, toán tử  
function isArray[myArray] {
  return myArray.constructor.toString[].indexOf["Array"] > -1;
}
3 luôn trả về một chuỗi [chứa kiểu của toán hạng].
Thuộc tính constructor[The constructor Property]Thuộc tính 
function isArray[myArray] {
  return myArray.constructor.toString[].indexOf["Array"] > -1;
}
6 trả về hàm tạo cho toàn bộ các biến JavaScript.
Bạn có thể kiểm tra thuộc tính cấu trúc để tìm ra nếu một đối tượng là một 




The JavaScript constructor Property

The constructor property returns the constructor function for a variable or an object.

document.getElementById["demo"].innerHTML = "john".constructor + "
" + [3.14].constructor + "
" + false.constructor + "
" + [1,2,3,4].constructor + "
" + {name:'john', age:34}.constructor + "
" + new Date[].constructor + "
" + function [] {}.constructor;
7 [chưa từ "Array"]:
Hoặc thậm chí dễ dàng hơn, bạn có thể kiểm tra nếu đối tượng đó là một hàm Array :
function isArray[myArray] {
  return myArray.constructor === Array;
}




JavaScript Array Object

This "home made" isArray[] function returns true when used on an array:

var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById["demo"].innerHTML = isArray[fruits]; function isArray[myArray] { return myArray.constructor === Array; }
Bạn có thể kiểm tra thuộc tính cấu trúc để tìm ra nếu một đối tượng là một 




The JavaScript constructor Property

The constructor property returns the constructor function for a variable or an object.

document.getElementById["demo"].innerHTML = "john".constructor + "
" + [3.14].constructor + "
" + false.constructor + "
" + [1,2,3,4].constructor + "
" + {name:'john', age:34}.constructor + "
" + new Date[].constructor + "
" + function [] {}.constructor;
6 [chưa từ khóa "Date"]:
Hoặc còn đơn giản hơn, bạn có thể kiểm tra nếu đối tượng là một hàm Date :Chuyển kiểu trong JavaScript[JavaScript Type Conversion]
Các biến JavaScript có thể được chuyển thành một biến mới với kiểu dữ liệu khác:Bằng cách sử dụng một trong các hàm JavaScript

Tự động bởi chính JavaScript


Chuyển số thành chuỗi




JavaScript Arrays

This "home made" isArray[] function returns true when used on an array:

var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById["demo"].innerHTML = isArray[fruits]; function isArray[myArray] { return myArray.constructor.toString[].indexOf["Array"] > -1; }
4 có thể chuyển chuỗi thành số.

Phương thức toàn cục 

function isArray[myArray] {
  return myArray.constructor.toString[].indexOf["Array"] > -1;
}
9 có thể chuyển số thành chuỗi.

Nó có thể được dùng trên bất kì kiểu số nào, số cố định, các biến, hoặc các biểu thức:

Phương thức của số 




JavaScript Arrays

This "home made" isArray[] function returns true when used on an array:

var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById["demo"].innerHTML = isArray[fruits]; function isArray[myArray] { return myArray.constructor.toString[].indexOf["Array"] > -1; }
0 cũng làm y hệt. 



JavaScript Arrays

This "home made" isArray[] function returns true when used on an array:

var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById["demo"].innerHTML = isArray[fruits]; function isArray[myArray] { return myArray.constructor.toString[].indexOf["Array"] > -1; }
5 [Không phải số].





The JavaScript typeof Operator

The typeof operator returns the type of a variable, object, function or expression.

document.getElementById["demo"].innerHTML = typeof "john" + "
" + typeof 3.14 + "
" + typeof NaN + "
" + typeof false + "
" + typeof [1,2,3,4] + "
" + typeof {name:'john', age:34} + "
" + typeof new Date[] + "
" + typeof function [] {} + "
" + typeof myCar + "
" + typeof null;
9


Trong chương phương thức của các số, bạn sẽ tìm thấy nhiều hơn các phương thức có thể sử dụng để chuyển số thành chuỗi:

Kiểu dữ liệu của NaN là numberKiểu dữ liệu của mảng là object
Kiểu dữ liệu của thời gian là objectKiểu dữ liệu của null là object
Kiểu dữ liệu của một biến không xác định là undefined *Kiểu dữ liệu của một biến chưa được gán giá trị cũng là undefined *

Tip: Bạn không thể dùng typeof để xác định nếu một đối tượng JavaScript là một mảng [hoặc một thời gian].

ví dụ

"John".constructor                // Returns function String[]  {[native code]}
[3.14].constructor                // Returns function Number[]  {[native code]}
false.constructor                 // Returns function Boolean[] {[native code]}
[1,2,3,4].constructor             // Returns function Array[]   {[native code]}
{name:'John',age:34}.constructor  // Returns function Object[]  {[native code]}
new Date[].constructor            // Returns function Date[]    {[native code]}
function [] {}.constructor        // Returns function Function[]{[native code]}
0
"John".constructor                // Returns function String[]  {[native code]}
[3.14].constructor                // Returns function Number[]  {[native code]}
false.constructor                 // Returns function Boolean[] {[native code]}
[1,2,3,4].constructor             // Returns function Array[]   {[native code]}
{name:'John',age:34}.constructor  // Returns function Object[]  {[native code]}
new Date[].constructor            // Returns function Date[]    {[native code]}
function [] {}.constructor        // Returns function Function[]{[native code]}
1


Kiểu dữ liệu của typeof




JavaScript Arrays

This "home made" isArray[] function returns true when used on an array:

var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById["demo"].innerHTML = isArray[fruits]; function isArray[myArray] { return myArray.constructor.toString[].indexOf["Array"] > -1; }
5 [Not a Number]:

ví dụ

"John".constructor                // Returns function String[]  {[native code]}
[3.14].constructor                // Returns function Number[]  {[native code]}
false.constructor                 // Returns function Boolean[] {[native code]}
[1,2,3,4].constructor             // Returns function Array[]   {[native code]}
{name:'John',age:34}.constructor  // Returns function Object[]  {[native code]}
new Date[].constructor            // Returns function Date[]    {[native code]}
function [] {}.constructor        // Returns function Function[]{[native code]}
2

Toán tử 
function isArray[myArray] {
  return myArray.constructor.toString[].indexOf["Array"] > -1;
}
3 không phải là một biến. Nó là một toán tử. Toán tử [ +-*/ ] không có bất kì kiểu dữ liệu.


Nhưng, toán tử  

function isArray[myArray] {
  return myArray.constructor.toString[].indexOf["Array"] > -1;
}
3 luôn trả về một chuỗi [chứa kiểu của toán hạng].



JavaScript Arrays

This "home made" isArray[] function returns true when used on an array:

var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById["demo"].innerHTML = isArray[fruits]; function isArray[myArray] { return myArray.constructor.toString[].indexOf["Array"] > -1; }
4 cũng có thể chuyển kiểu boolean thành số.

"John".constructor                // Returns function String[]  {[native code]}
[3.14].constructor                // Returns function Number[]  {[native code]}
false.constructor                 // Returns function Boolean[] {[native code]}
[1,2,3,4].constructor             // Returns function Array[]   {[native code]}
{name:'John',age:34}.constructor  // Returns function Object[]  {[native code]}
new Date[].constructor            // Returns function Date[]    {[native code]}
function [] {}.constructor        // Returns function Function[]{[native code]}
3

Thuộc tính constructor[The constructor Property]


Phương thức toàn cục 




JavaScript Arrays

This "home made" isArray[] function returns true when used on an array:

var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById["demo"].innerHTML = isArray[fruits]; function isArray[myArray] { return myArray.constructor.toString[].indexOf["Array"] > -1; }
4 có thể được dùng để chuyển kiểu thời gian thành số.



JavaScript Arrays

This "home made" isArray[] function returns true when used on an array:

var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById["demo"].innerHTML = isArray[fruits]; function isArray[myArray] { return myArray.constructor.toString[].indexOf["Array"] > -1; }
4 có thể được dùng để chuyển kiểu thời gian thành số.

"John".constructor                // Returns function String[]  {[native code]}
[3.14].constructor                // Returns function Number[]  {[native code]}
false.constructor                 // Returns function Boolean[] {[native code]}
[1,2,3,4].constructor             // Returns function Array[]   {[native code]}
{name:'John',age:34}.constructor  // Returns function Object[]  {[native code]}
new Date[].constructor            // Returns function Date[]    {[native code]}
function [] {}.constructor        // Returns function Function[]{[native code]}
4


Phương thức của thời gian 




JavaScript Arrays

This "home made" isArray[] function returns true when used on an array:

var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById["demo"].innerHTML = isArray[fruits]; function isArray[myArray] { return myArray.constructor.toString[].indexOf["Array"] > -1; }
9 cũng làm điều tương tự.



JavaScript Arrays

This "home made" isArray[] function returns true when used on an array:

var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById["demo"].innerHTML = isArray[fruits]; function isArray[myArray] { return myArray.constructor.toString[].indexOf["Array"] > -1; }
9 cũng làm điều tương tự.

"John".constructor                // Returns function String[]  {[native code]}
[3.14].constructor                // Returns function Number[]  {[native code]}
false.constructor                 // Returns function Boolean[] {[native code]}
[1,2,3,4].constructor             // Returns function Array[]   {[native code]}
{name:'John',age:34}.constructor  // Returns function Object[]  {[native code]}
new Date[].constructor            // Returns function Date[]    {[native code]}
function [] {}.constructor        // Returns function Function[]{[native code]}
5

[Chuyển kiểu tự động]Automatic Type Conversion


Mỗi khi JavaScript cố gắng tính toán trên một kiểu dữ liệu "sai", nó sẽ cố gắng chuyển giá trị thành "đúng" kiểu.

Kết quả không phải lúc nào cũng như bạn mong muốn:

"John".constructor                // Returns function String[]  {[native code]}
[3.14].constructor                // Returns function Number[]  {[native code]}
false.constructor                 // Returns function Boolean[] {[native code]}
[1,2,3,4].constructor             // Returns function Array[]   {[native code]}
{name:'John',age:34}.constructor  // Returns function Object[]  {[native code]}
new Date[].constructor            // Returns function Date[]    {[native code]}
function [] {}.constructor        // Returns function Function[]{[native code]}
6
"John".constructor                // Returns function String[]  {[native code]}
[3.14].constructor                // Returns function Number[]  {[native code]}
false.constructor                 // Returns function Boolean[] {[native code]}
[1,2,3,4].constructor             // Returns function Array[]   {[native code]}
{name:'John',age:34}.constructor  // Returns function Object[]  {[native code]}
new Date[].constructor            // Returns function Date[]    {[native code]}
function [] {}.constructor        // Returns function Function[]{[native code]}
7

[Chuyển kiểu chuỗi tự động]Automatic String Conversion


JavaScript tự động gọi hàm 




JavaScript Arrays

This "home made" isArray[] function returns true when used on an array:

var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById["demo"].innerHTML = isArray[fruits]; function isArray[myArray] { return myArray.constructor.toString[].indexOf["Array"] > -1; }
0 của biến khi bạn cố gắng "xuất ra" một đối tượng hoặc một biến:



JavaScript Arrays

This "home made" isArray[] function returns true when used on an array:

var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById["demo"].innerHTML = isArray[fruits]; function isArray[myArray] { return myArray.constructor.toString[].indexOf["Array"] > -1; }
0 của biến khi bạn cố gắng "xuất ra" một đối tượng hoặc một biến:

"John".constructor                // Returns function String[]  {[native code]}
[3.14].constructor                // Returns function Number[]  {[native code]}
false.constructor                 // Returns function Boolean[] {[native code]}
[1,2,3,4].constructor             // Returns function Array[]   {[native code]}
{name:'John',age:34}.constructor  // Returns function Object[]  {[native code]}
new Date[].constructor            // Returns function Date[]    {[native code]}
function [] {}.constructor        // Returns function Function[]{[native code]}
8


Số và các boolean cũng được chuyển đổi, nhưng điều này cũng không được hiển thị rõ ràng cho lắm:

"John".constructor                // Returns function String[]  {[native code]}
[3.14].constructor                // Returns function Number[]  {[native code]}
false.constructor                 // Returns function Boolean[] {[native code]}
[1,2,3,4].constructor             // Returns function Array[]   {[native code]}
{name:'John',age:34}.constructor  // Returns function Object[]  {[native code]}
new Date[].constructor            // Returns function Date[]    {[native code]}
function [] {}.constructor        // Returns function Function[]{[native code]}
9

Bảng chuyển kiểu JavaScript[JavaScript Type Conversion Table]

Giá trị

gốc

Chuyển thành

số

Chuyển thành

số

Chuyển thành

số

chuỗi0 Boolean chuỗi
Boolean1 false Boolean
0 0 false chuỗi
1 1 Boolean Boolean
false 0 false Boolean
false 0 false Boolean
Boolean1 Boolean Boolean
false false "false" chuỗi
Boolean Booleanfalse Boolean
false false "false" Boolean
false 0 false chuỗi
Boolean20 Boolean Boolean
false false false Boolean
false 0 false Boolean
false 20 Boolean Boolean
false false "false" Boolean
false false false Boolean
false false "false" Boolean
false false "false" Boolean
false false "false" Boolean
false 0 "false" chuỗi
Booleanfalse "false" chuỗi


Boolean

false  biểu thi giá trị [một vài] lập trình viên có thể không mong đợi.


Bài Viết Liên Quan

Chủ Đề