JavaScript对象

JS对象

JavaScript中的所有事物都是对象:字符串、数值、数组、函数…
此外,JavaScript允许自定义对象。

创建JavaScript对象

创建新对象有两种不同的方法:

  • 创建直接的实例。
  • 使用函数来定义对象,然后创建新的对象实例。

创建直接的实例

下面这个例子创建了对象的一个新实例,并向其添加了四个属性:

person = new Object();      // 创建一个直接对象
person.firstname="Bill";
person.lastname="Gates";
person.age=56;
person.eyecolor="blue";

替代语法(使用对象 literals):

person={firstname:"John",lastname:"Doe",age:50,eyecolor:"blue"};


使用函数定义对象

上面例子的等价语法为:

function person(firstname,lastname,age,eyecolor)
{
    this.firstname=firstname;
    this.lastname=lastname;
    this.age=age;
    this.eyecolor=eyecolor;
}

var myFather = new person("Bill","Gates",56,"blue");    // 创建一个函数对象


JavaScript类

JavaScript是面向对象的语言,但JavaScript不使用类。
在JavaScript中,不会创建类,也不会通过类来创建对象(就像在其他面向对象的语言中那样)。
JavaScript基于prototype,而不是基于类的

for…in循环

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

var person = {fname:"Bill",lname:"Gates",age:56};

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


Number对象

JavaScript不是类型语言。与许多其他编程语言不同,JavaScript不定义不同类型的数字,比如整数、短、长、浮点等等。
JavaScript中的所有数字都存储为64位的浮点数。
整数(不使用小数点或指数计数法)最多为 15 位。
小数的最大位数是 17,但是浮点运算并不总是 100% 准确:


浮点数范围:

as  large  as ±1.7976931348623157 × 10308次方(Number.MAX_VALUE)
as small as ±5 × 10的−324次方(Number.MIN_VALUE)

参考链接


精确整数范围:
The JavaScript number format allows you to exactly represent all integers between
−9007199254740992 and 9007199254740992 (即正负2的53次方)


数组索引还有位操作:
正负2的31次方

String对象

为字符串添加样式

<!DOCTYPE html>
<html>
<body>

<script type="text/javascript">

var txt="Hello World!"

<!-- 粗体、斜体 -->
document.write("<p>Bold: " + txt.bold() + "</p>")
document.write("<p>Italic: " + txt.italics() + "</p>")

<!-- 改变字体颜色、大小-->
document.write("<p>Fontcolor: " + txt.fontcolor("Red") + "</p>")
document.write("<p>Fontsize: " + txt.fontsize(16) + "</p>")

<!-- 转换大小写 -->
document.write("<p>Lowercase: " + txt.toLowerCase() + "</p>")
document.write("<p>Uppercase: " + txt.toUpperCase() + "</p>")

<!-- 上下标 -->
document.write("<p>Subscript: " + txt.sub() + "</p>")
document.write("<p>Superscript: " + txt.sup() + "</p>")

<!-- 添加链接 -->
document.write("<p>Link: " + txt.link("http://www.w3school.com.cn") + "</p>")

</script>

</body>
</html>


match方法

如果在字符串中找到,则返回特定的字符;如果找不到,则返回null。

<!DOCTYPE html>
<html>
<body>

<script type="text/javascript">

var str="Hello world!"

<!-- 输出world -->
document.write(str.match("world") + "<br />")

<!-- 输出world! -->
document.write(str.match("world!"))

<!-- 输出null-->
document.write(str.match("worlld") + "<br />")

</script>

</body>
</html>


Date对象

获取当前日期和时间(Date())

<!DOCTYPE html>
<html>
<body>

<script type="text/javascript">

document.write(Date())

</script>

</body>
</html>


获取从1970年1月1日至今的毫秒数(getTime())

<!DOCTYPE html>
<html>
<body>

<script type="text/javascript">

var d = new Date(); <!-- 先创建一个Date对象 -->
document.write("从 1970/01/01 至今已过去 " + d.getTime() + " 毫秒");

</script>

</body>
</html>


获取今天是星期几(getDay())

<!DOCTYPE html>
<html>
<body>

<script type="text/javascript">

var d = new Date()
var weekday = new Array(7)
weekday[0]="星期日"
weekday[1]="星期一"
weekday[2]="星期二"
weekday[3]="星期三"
weekday[4]="星期四"
weekday[5]="星期五"
weekday[6]="星期六"

document.write("今天是" + weekday[d.getDay()])

</script>

</body>
</html>


设置年月日(setFullYear())

<!DOCTYPE html>
<html>
<body>

<script type="text/javascript">

var d = new Date()
d.setFullYear(1992,10,3)    <!-- 月份设置为11月 !!! -->
document.write(d)

</script>

</body>
</html>

注意:表示月份的参数介于0到11之间。也就是说,如果希望把月设置为11月,则参数应该是10。

比较两个日期(>, <)

var myDate=new Date();
myDate.setFullYear(2008,8,9);

var today = new Date();

if (myDate > today)
{
    alert("Today is before 9th August 2008");
}
else
{
    alert("Today is after 9th August 2008");
}


Array对象

for…in循环数组

<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
var x
var mycars = new Array()
mycars[0] = "Saab"
mycars[1] = "Volvo"
mycars[2] = "BMW"

for (x in mycars)
{
    document.write(mycars[x] + "<br />")
}
</script>
</body>
</html>


合并两个数组(oncat())

<!DOCTYPE html>
<html>
<body>

<script type="text/javascript">

var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"

var arr2 = new Array(3)
arr2[0] = "James"
arr2[1] = "Adrew"
arr2[2] = "Martin"

<!-- 输出"George,John,Thomas,James,Adrew,Martin" -->
document.write(arr.concat(arr2))

</script>

</body>
</html>


将数组元素组成一个字符串(join())

<!DOCTYPE html>
<html>
<body>

<script type="text/javascript">

var arr = new Array(3);
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"

<!-- 输出"George,John,Thomas" -->
document.write(arr.join());

<!-- 输出"George-00-John-00-Thomas" -->
document.write(arr.join("-00-"));

</script>

</body>
</html>


字符串排序(sort())

<!DOCTYPE html>
<html>
<body>

<script type="text/javascript">

var arr = new Array(6)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
arr[3] = "James"
arr[4] = "Adrew"
arr[5] = "Martin"

<!-- 输出"Adrew,George,James,John,Martin,Thomas" -->
document.write(arr.sort())

</script>

</body>
</html>


字符值排序(sort(compareFun))

<!DOCTYPE html>
<html>
<body>

<script type="text/javascript">

function sortNumber(a, b)
{
    return a - b
}

var arr = new Array(6)
arr[0] = "10"
arr[1] = "5"
arr[2] = "40"
arr[3] = "25"
arr[4] = "1000"
arr[5] = "1"

<!-- 输出"1,5,10,25,40,1000" -->
document.write(arr.sort(sortNumber))

</script>

</body>
</html>


Math对象

Match.round()

<!DOCTYPE html>
<html>
<body>

<script type="text/javascript">

<!-- 输出 1 -->
document.write(Math.round(0.50) + "<br />")
<!-- 输出 0 -->
document.write(Math.round(0.49) + "<br />")
<!-- 输出 -4 -->
document.write(Math.round(-4.40) + "<br />")
<!-- 输出 -5 -->
document.write(Math.round(-4.60))

</script>

</body>
</html>


Math.random()

random()返回0到1之间的随机数。

<!DOCTYPE html>
<html>
<body>

<script type="text/javascript">

document.write(Math.random())

</script>

</body>
</html>


算数值

JavaScript 提供 8 种可被 Math 对象访问的算数值:

定义
常数Math.E
圆周率Math.PI
2 的平方根Math.SQRT2
1/2 的平方根Math.SQRT1_2
2 的自然对数Math.LN2
10 的自然对数Math.LN10
以 2 为底的 e 的对数Math.LOG2E
以 10 为底的 e 的对数Math.LOG10E


RegExp对象

RegExp是正则表达式的缩写。
当您检索某个文本时,可以使用一种模式来描述要检索的内容。RegExp就是这种模式。
RegExp对象有3个方法:test()、exec() 以及 compile()。

test方法

test()方法检索字符串中的指定值。返回值是true或false。

<!DOCTYPE html>
<html>
<body>

<script type="text/javascript">

var patt1 = new RegExp("e");
document.write(patt1.test("The best things in life are free"));   <!-- 输出true -->

</script>

</body>
</html>


exec方法

exec()方法检索字符串中的指定值。返回值是被找到的值。如果没有发现匹配,则返回null。
您可以向RegExp对象添加第二个参数,以设定检索。例如,如果需要找到所有某个字符的所有存在,则可以使用 “g” 参数 (“global”)。

<!DOCTYPE html>
<html>
<body>

<script type="text/javascript">

var patt1=new RegExp("e","g");  <!-- 进行全局搜索"g" -->

do
{
    result = patt1.exec("The best things in life are free");
    document.write(result);     <!-- 输出eeeeeenull -->
}
while (result!=null)

</script>

</body>
</html>


compile方法

compile()方法用于改变RegExp。
compile()既可以改变检索模式,也可以添加或删除第二个参数。

<!DOCTYPE html>
<html>
<body>

<script type="text/javascript">

var patt1=new RegExp("e");
document.write(patt1.test("The best things in life are free"));     <!-- 输出true -->

patt1.compile("d");                                                 <!-- 改变RegExp -->
document.write(patt1.test("The best things in life are free"));     <!-- 输出false -->

</script>

</body>
</html>


更多请参考:W3School

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值