目录
定义方法:
方法就是把函数放在对象的里面,对象只有两个东西:属性和方法
- <script>
- var biubiu={
- //属性
- name:'biu',
- birth:1999,
- //方法
- age:function () {
- //今年-出生的年
- var now=new Date().getFullYear();
- return now-this.birth;
- }
- }
-
- script>
调用方法一定要带()

this.代表什么?拆开上面的代码看看
- <script>
- function getAge() {
- //今年-出生的年
- var now=new Date().getFullYear();
- return now-this.birth;
- }
- var biubiu={
- //属性
- name:'biu',
- birth:1999,
- //方法
- age:getAge
- }
-
- script>

在Java中 this 是无法指向的,是默认指向调用它的那个对象的;在 js 中可以使用apply控制 this 指向
- <script>
- function getAge() {
- //今年-出生的年
- var now=new Date().getFullYear();
- return now-this.birth;
- }
- var biubiu={
- //属性
- name:'biu',
- birth:1999,
- //方法
- age:getAge
- }
- getAge.apply(biubiu,[]);//空参:this指向biubiu这个对象
- script>

标准对象:利用typeof进行判断

- <script>
- var now = new Date();//获取当前时间
- now.getFullYear();//年份
- now.getMonth();//月份 0-11
- now.getDate();//日
- now.getDay();//星期几
- now.getHours();//时
- now.getMinutes();//分
- now.getSeconds();//秒
- now.getTime();//时间戳 全世界统一 1970 1.1 0:00:00 至今的毫秒数
- script>


通过时间戳获取当前时间:
注意:调用是一个方法而不是属性

早期,所有的数据传输习惯使用XML文件
JSON是什么?
JSON(JavaScript Object Notation, JS 对象简谱) 是一种轻量级的数据交换格式。
简单来说:json 就是一种在各个编程语言中流通的数据格式,负责不同编程语言中的数据传递和交互。
在javascript中,一切皆为对象,任何 js 支持的类型都可以用 JSON 表示,只需要记准格式:
JSON 字符串和 js 对象转化:
对象转换成json字符串:
- <script>
- var user={
- name:"biubiu",
- age:3,
- sex:'女'
- }
- //对象转换成json字符串
- var jsonUser=JSON.stringify(user);
- script>

json字符串转化为对象:
- <script>
- //json字符串转化为对象
- var obj=JSON.parse('{"name":"biubiu","age":3,"sex":"女"}')
- script>

JSON和js的区别:
var obj={a:'hello',b:'hihi'}; --> 对象
var json='{'a':'hello','b':'hihi'}' --> json字符串