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ị:
0The 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;
1The 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;
2The 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;
3The 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;
4The 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;
Có 6 kiểu đối tượng:
5The 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;
6The 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;
7The 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;
8The 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;
9The 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;
0function isArray[myArray] { return myArray.constructor.toString[].indexOf["Array"] > -1; }
Và hai kiểu dữ liệu không thể chứa giá trị:
1function isArray[myArray] { return myArray.constructor.toString[].indexOf["Array"] > -1; }
2function isArray[myArray] { return myArray.constructor.toString[].indexOf["Array"] > -1; }
Toán tử typeof
Bạn có thể sử dụng toán tử
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;
}
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ử
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;
}
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ử
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;
}
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
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;
}
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;
1Xin 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 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 * | 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.
function isArray[myArray] {
return myArray.constructor.toString[].indexOf["Array"] > -1;
}
Nhưng, toán tử
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;
}
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;
6Thuộc tính constructor[The constructor Property]
Thuộc tính
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;
}
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;
7The constructor property returns the constructor function for a variable or an
object.
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
7 [chưa từ "Array"]:
The JavaScript constructor Property
" +
[3.14].constructor + "
" +
false.constructor + "
" +
[1,2,3,4].constructor + "
" +
{name:'john', age:34}.constructor + "
" +
new Date[].constructor + "
" +
function [] {}.constructor;
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;
8Xin hãy quan sát:
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 * | 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ử 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ử 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 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 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 : |
| 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 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
9 có thể chuyển số thành chuỗi.function isArray[myArray] {
return myArray.constructor.toString[].indexOf["Array"] > -1;
}
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ố This "home made" isArray[] function returns true when used on an array:
0 cũng làm y hệt.
JavaScript Arrays
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 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 * | 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]}
2Toá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;
}
Nhưng, toán tử
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;
}
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]}
3Thuộc tính constructor[The constructor Property]
This "home made" isArray[] function returns true when used on an array:
Phương thức toàn cục
4 có thể được dùng để chuyển kiểu thời gian thành số.
JavaScript Arrays
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]}
4This "home made" isArray[] function returns true when used on an array:
Phương thức của thời gian
9 cũng làm điều tương tự.
JavaScript Arrays
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
This "home made" isArray[] function returns true when used on an array:
JavaScript tự động gọi hàm
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
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]}
9Bảng chuyển kiểu JavaScript[JavaScript Type Conversion Table]
chuỗi | 0 | Boolean | chuỗi |
Boolean | 1 | false | Boolean |
0 | 0 | false | chuỗi |
1 | 1 | Boolean | Boolean |
false | 0 | false | Boolean |
false | 0 | false | Boolean |
Boolean | 1 | Boolean | Boolean |
false | false | "false" | chuỗi |
Boolean | Boolean | false | Boolean |
false | false | "false" | Boolean |
false | 0 | false | chuỗi |
Boolean | 20 | 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 |
Boolean | false | "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.