您的位置:首页 > Web前端 > JavaScript

javascript。。。。。。。

2015-09-14 09:36 691 查看
创建新的 HTML 元素

创建新的 HTML 元素

如需向 HTML DOM 添加新元素,您必须首先创建该元素(元素节点),然后向一个已存在的元素追加该元素。

JavaScript 类

JavaScript 是面向对象的语言,但 JavaScript 不使用类。

在 JavaScript 中,不会创建类,也不会通过类来创建对象(就像在其他面向对象的语言中那样)。

JavaScript 基于 prototype,而不是基于类的。

prototype :原型

JavaScript for...in 循环

JavaScript for...in 语句循环遍历对象的属性。

语法

for (variable in object)

{

code to be executed

}

注意: for...in 循环中的代码块将针对每个属性执行一次。
实例

循环遍历对象的属性:
实例
var person={fname:"John",lname:"Doe",age:25};

for (x in person)
{
txt=txt + person[x];
}


所有 JavaScript 数字均为 64 位

创建新方法

原型是JavaScript全局构造函数。它可以构建新Javascript对象的属性和方法。

<!DOCTYPE html>
<html>
<body>

<p id="demo">Click the button to create an array, call the new ucase() method, and display the result.</p>

<button onclick="myFunction()">Try it</button>

<script>
Array.prototype.myUcase=function()
{
for (i=0;i<this.length;i++)
{
this[i]=this[i].toUpperCase();
}
}

function myFunction()
{
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.myUcase();
var x=document.getElementById("demo");
x.innerHTML=fruits;
}
</script>

</body>
</html>


JavaScript Boolean(布尔) 对象

TBoolean(布尔)对象用于将非布尔值转换为布尔值(true 或者 false)。

<!DOCTYPE html>
<html>
<body>

<script>
var b1=new Boolean(0);
var b2=new Boolean(1);
var b3=new Boolean("");
var b4=new Boolean(null);
var b5=new Boolean(NaN);
var b6=new Boolean("false");

document.write("0 is boolean "+ b1 +"<br>");
document.write("1 is boolean "+ b2 +"<br>");
document.write("An empty string is boolean "+ b3 + "<br>");
document.write("null is boolean "+ b4+ "<br>");
document.write("NaN is boolean "+ b5 +"<br>");
document.write("The string 'false' is boolean "+ b6 +"<br>");
</script>

</body>
</html>


0 is boolean false

1 is boolean true

An empty string is boolean false

null is boolean false

NaN is boolean false

The string 'false' is boolean true

创建 Boolean 对象

Boolean 对象代表两个值:"true" 或者 "false"

下面的代码定义了一个名为 myBoolean 的布尔对象:

var myBoolean=new Boolean();

如果布尔对象无初始值或者其值为:

0
-0
null
""
false
undefined
NaN

那么对象的值为 false。否则,其值为 true(即使当自变量为字符串 "false" 时)!

test()

The test()方法搜索字符串指定的值,根据结果并返回真或假。

下面的示例是从字符串中搜索字符 "e" :

实例

var patt1=new RegExp("e");

document.write(patt1.test("The best things in life are free"));
由于该字符串中存在字母 "e",以上代码的输出将是:

true
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: