JS中ES6类和对象
【摘要】
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body&...
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
/*function Person(myName, myAge)
{
//实例属性
this.name=myName;
this.age=myAge;
//实例方法
this.say=function()
{
console.log(this.name, this.age);
}
//记住,静态方法是通过构造函数名作为对象的哈.
Person.num=666;
Person.run=function()
{
console.log("run");
}
}
let p=new Person("cyg",20);
p.say();
console.log(Person.num);
Person.run();
*/
//以下es6
/*class Person
{
constructor(myName, myAge)
{
this.name = myName;
this.age = myAge;
}
//es6中实例的和静态的属性与方法.
//name="cyg";
//age=20;
say()
{
console.log(this.name, this.age);
}
//以下静态
static num=666;
static run()
{
console.log("run");
}
}
let p = new Person("zs", 18);
p.say();
console.log(Person.num);
Person.run();*/
/* class Person
{ // 在ES6标准中添加实例属性都需要在constructor中添加
constructor()
{
this.name="cyg";
this.age=20;
}
say()
{
console.log(this.name, this.age);
}
}
let p = new Person();
console.log(p);
*/
/*class Person
{
// 在ES标准中static只支持定义静态方法不支持定义静态变量
// 大部分浏览器不支持静态变量的做法.会报错
// static num=666;
static run()
{
console.log("run");
}
}
let p = new Person();
//console.log(Person.num);
console.log(Person.run());*/
/*function Person(myName, myAge)
{
// 实例属性
this.name = myName;
this.age = myAge;
// 实例方法
this.hi = function () {
console.log("hi");
}
}
Person.prototype.say=function()
{
console.log(this.name, this.age);
}
let p = new Person("lnj", 34);
console.log(p.say());*/
//类没有去原型里面找
</script>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
</style>
</head>
<body>
<script type="text/javascript">
/*function Person(myName, myAge)
{
this.name=myName;
this.age=myAge;
this.hi=function()
{
console.log("h1");
}
}
/*Person.prototype.type="cyg";
Person.prototype.say=function()
{
console.log(this.name, this.age);
};
*/
//也可以这样写.
/*Person.prototype={
constructor:Person,
type:"cyg",
say:function()
{
console.log(this.name, this.age);
}
};*/
class Person
{
constructor(myName, myAge)
{
this.name = myName;
this.age = myAge;
this.hi = function () {
console.log("hi");
}
}
run()
{
console.log("run");
}
}
// Person.prototype.type = "人";
// Person.prototype.say = function () {
// console.log(this.name, this.age);
// };
let obj={constructor:Person,type:"cyg",say:function()
{
console.log(this.name, this.age);
}};
Person.prototype=obj;
let p = new Person("lnj", 34);
console.log(p);
</script>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
文章来源: blog.csdn.net,作者:贵哥的编程之路(陈业贵),版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/qq_37805832/article/details/108395473
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)