JS笔记-006-JS对象-数字-字符串-日期-数组-逻辑

JS对象

创建 JavaScript 对象

通过 JavaScript,您能够定义并创建自己的对象。

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

  • 定义并创建对象的实例(直接创建对象)

    person=new Object();
    person.firstname="Bill";
    person.lastname="Gates";
    person.age=56;
    person.eyecolor="blue";
  • 使用函数来定义对象,然后创建新的对象实例,类似于构造方法

    person={firstname:"John",lastname:"Doe",age:50,eyecolor:"blue"};
  • 使用构造器创建对象,和java类似了,直接创建对象,赋值调用封装方法

<!DOCTYPE html>
<html>
<body>

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

myFather=new person("Bill","Gates",56,"blue");

document.write(myFather.firstname + " is " + myFather.age + " years old.");
</script>

</body>
</html>
把属性添加到 JavaScript 对象

您可以通过为对象赋值,向已有对象添加新属性:假设 personObj 已存在 - 您可以为其添加这些新属性:firstname、lastname、age 以及 eyecolor:

person.firstname="Bill";
person.lastname="Gates";
person.age=56;
person.eyecolor="blue";

x=person.firstname;
把方法添加到 JavaScript 对象

方法只不过是附加在对象上的函数。
在构造器函数内部定义对象的方法:

<!DOCTYPE html>
<html>
<body>
<script>
function person(firstname,lastname,age,eyecolor)
{
this.firstname=firstname;
this.lastname=lastname;
this.age=age;
this.eyecolor=eyecolor;

this.changeName=changeName;
function changeName(name)
{
this.lastname=name;
}
}
myMother=new person("Steve","Jobs",56,"green");
myMother.changeName("Ballmer");
document.write(myMother.lastname);
</script>

</body>
</html>

JS数字

JavaScript Number 对象

JavaScript 只有一种数字类型。可以使用也可以不使用小数点来书写数字。
极大或极小的数字可通过科学(指数)计数法来写:

var y=123e5;    // 12300000
var z=123e-5;   // 0.00123

所有 JavaScript 数字均为 64 位

JavaScript 不是类型语言。与许多其他编程语言不同,JavaScript 不定义不同类型的数字,比如整数、短、长、浮点等等。
JavaScript 中的所有数字都存储为根为 10 的 64 位(8 比特),浮点数。

JS字符串属性

转大写
txt.toUpperCase()

更改样式

<html>
<body>

<script type="text/javascript">

var txt="Hello World!"

document.write("<p>Big: " + txt.big() + "</p>")
document.write("<p>Small: " + txt.small() + "</p>")

document.write("<p>Bold: " + txt.bold() + "</p>")
document.write("<p>Italic: " + txt.italics() + "</p>")

document.write("<p>Blink: " + txt.blink() + " (does not work in IE)</p>")
document.write("<p>Fixed: " + txt.fixed() + "</p>")
document.write("<p>Strike: " + txt.strike() + "</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>

JS日期

  • 返回当前日期
    <html>
    <body>
    
- getTime(),返回从 1970 年 1 月 1 日至今的毫秒数。
- setFullYear(),如何使用 setFullYear() 设置具体的日期。
-  toUTCString(),使用 toUTCString() 将当日的日期(根据 UTC)转换为字符串。
- getDay(),使用 getDay() 和数组来显示星期,而不仅仅是数字
- 显示一个钟表

##### 定义日期

var myDate=new Date()


### JS数组

##### 创建数组

##### For...In循环数组

``` ##### 合并数组,arr.concat(arr2) ```
##### 将数组元素组成一个字符串,join

#####  sort() 方法从字面上对数组进行排序,自然排序(数字可以针对大小排序)

### JS逻辑(Boolean对象)
您可以将 Boolean 对象理解为一个产生逻辑值的对象包装器。
Boolean(逻辑)对象用于将非逻辑值转换为逻辑值(true 或者 false)。

创建Boolean对象

var myBoolean=new Boolean()

如果逻辑对象无初始值或者其值为 0、-0、null、""、false、undefined 或者 NaN,那么对象的值为 false。否则,其值为 true(即使当自变量为字符串 "false" 时)!

var myBoolean=new Boolean();
var myBoolean=new Boolean(0);
var myBoolean=new Boolean(null);
var myBoolean=new Boolean(“”);
var myBoolean=new Boolean(false);
var myBoolean=new Boolean(NaN);

初始值全部为true

var myBoolean=new Boolean(1);
var myBoolean=new Boolean(true);
var myBoolean=new Boolean(“true”);
var myBoolean=new Boolean(“false”);
var myBoolean=new Boolean(“Bill Gates”);


### JS算数
- Math 对象的 round 方法对一个数进行四舍五入。

document.write(Math.round(4.7))

- Math 对象的 random() 方法来返回一个介于 0 和 1 之间的随机数

document.write(Math.random())

- 使用了 Math 对象的 floor() 方法和 random() 来返回一个介于 0 和 10 之间的随机数

document.write(Math.floor(Math.random()*11))