內建 javascript 建構函式?
在這篇文章中,我們將討論內建 JavaScript 建構函式以及相應 JavaScript 示例。
Javascript 提供了一些用於原生物件的內建建構函式。這些內建函式包括 Object()、String()、Boolean()、RegExp()、Number()、Array()、Function()、Date()。
注意我們不能在這些內建建構函式中包含 Math 物件,因為 Math 是一個全域性物件。不能對 Math 使用“new”關鍵字。
讓我們藉助本文中進一步提供的示例,以更好的方式理解這個概念。
示例 1
這是一個示例程式,用於說明內建建構函式 String()。
<!DOCTYPE html>
<html>
<head>
<title>Built-in JavaScript Constructors</title>
</head>
<body style="text-align : center">
<h3>String() - Built-in JavaScript Constructor</h3>
<p id="valueToString"></p>
<script>
var value1 = 3.14;
var value2 = String(value1);
var result = value1;
document.getElementById("valueToString").innerHTML = "The converted value to string is : " + result;
</script>
</body>
</html>
執行上述程式碼後,將生成以下輸出。
示例 2
這是一個示例程式,用於說明內建建構函式 Number()。
<!DOCTYPE html>
<html>
<head>
<title>Built-in JavaScript Constructors</title>
</head>
<body style="text-align : center">
<h3>Number() - Built-in JavaScript Constructor</h3>
<p id='Built-in'></p>
<script>
let a = Number("100");
let b = Number("100.234");
let c = Number(true); //return 0 or 1 for boolean
let d = Number("true"); //returns a number or NaN
let e = Number(new Date()); //returns number of milli seconds from Jan 1 1970
let f = Number([1.2]);
document.getElementById('Built-in').innerHTML = `Number("100") is ${a} ${'<br/>'} Number("100.234") is ${b} ${'<br/>'} Number(true) is ${c} ${'<br/>'} Number("true") is ${d} ${'<br/>'} Number(new Date()) is ${e} ${'<br/>'} Number([1.2]) is ${f} `;
</script>
</body>
</html>
執行上述程式碼後,將生成以下輸出。
示例 3
這是一個示例程式,用於說明內建建構函式 Boolean()。
<!DOCTYPE html>
<html>
<head>
<title>Built-in JavaScript Constructors</title>
</head>
<body style="text-align : center">
<h3>Boolean() - Built-in JavaScript Constructor</h3>
<p id="bool"></p>
<script>
var value1 = 0;
var value2 = '';
var value3 = null;
var value4 = 'Hello';
var value5 = 1234;
document.getElementById("bool").innerHTML = "The boolean value for the value (0) is : " + Boolean(value1) + '<br/>' + "The boolean value for the value ('') is : " + Boolean(value2) + '<br/>' +"The boolean value for the value (null) is : " + Boolean(value3) + '<br/>' +"The boolean value for the value ('Hello') is : " + Boolean(value4) + '<br/>' +"The boolean value for the value (1234) is : " + Boolean(value5);
</script>
</body>
</html>
執行上述程式碼後,將生成以下輸出。
示例 4
這是一個示例程式,用於說明內建建構函式 Object()。每個建構函式都是 Object 建構函式的一部分。
<!DOCTYPE html>
<html>
<head>
<title>Built-in JavaScript Constructors</title>
</head>
<body style="text-align : center">
<h3>Built-in JavaScript Constructors</h3>
<p id="valueToString"></p>
<script>
const a1 = new String();
const a2 = new Number();
const a3 = new Boolean();
const a4 = new Object();
const a5 = new Array();
const a6 = new Date();
document.getElementById("valueToString").innerHTML = `a1 is ${typeof a1} ${'<br/>'} a2 is ${typeof a2} ${'<br/>'} a3 is ${typeof a3} ${'<br/>'} a4 is ${typeof a4} ${'<br/>'} a5 is ${typeof a5} ${'<br/>'} a6 is ${typeof a6} `;
</script>
</body>
</html>
執行上述程式碼後,將生成以下輸出。
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP