面向对象初识

来源:互联网 发布:数控编程和程序员 编辑:程序博客网 时间:2024/05/21 00:50

<!DOCTYPE html>

<html>

<head>

<metacharset="UTF-8">

<title></title>

</head>

<body>

<scripttype="text/javascript">

var x = 0;

functiontest(){

// 键值-key value  属性

// 函数(面向过程)-> 方法(面向)

}

// 字面量

varhuang = {

// 属性

name:"大黄",

sex:"男",

age:21,

// 方法

sayHello:function(){

alert("hello");

}

};

// 1.通过.获取属性内容

console.log(huang.name);

huang.sayHello();

// 2.通过【】获取属性内容

// 区别:.必须知道属性具体的名字才能取值, 【】可以用变量取值

varx = "name";

huang[x];

console.log(huang["age"]);

// document.getElementById();

// 构造函数

vardate = new Date();

date.getDay();

// console.log(day);

//构造函数内部必须使用this关键字赋值

// 默认返回对象本身

functionAnimal(){

this.name = "字典";

this.hi= function(){

alert("你好,约么?");

}

}

var dog = new Animal();

var cat = new Animal();

dog.hi();

</script>

</body>

</html>

0 0
原创粉丝点击