JavaScript-属性方法分类
  TEZNKK3IfmPf 12天前 18 0

在 JavaScript 中属性和方法分类两类

在企业开发中通过实例对象访问的属性,我们就称之为 ​​实例属性​

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Demo</title>
<script type="text/javascript">
function Person() {
this.name = "BNTang";
this.say = function () {
console.log("hello world");
}
}

let person = new Person();
console.log(person.name);
</script>
</head>
<body>
</body>
</html>

在企业开发中通过实例对象调用的方法,我们就称之为 ​​实例方法​

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Demo</title>
<script type="text/javascript">
function Person() {
this.name = "BNTang";
this.say = function () {
console.log("hello world");
}
}

let person = new Person();
person.say();
</script>
</head>
<body>
</body>
</html>

静态属性与静态方法

在企业开发中通过构造函数访问的属性,我们就称之为 ​​静态属性​

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Demo</title>
<script type="text/javascript">
function Person() {
this.name = "BNTang";
this.say = function () {
console.log("hello world");
}
}

// 构造函数也是一个"对象", 所以我们也可以给构造函数动态添加属性和方法
Person.num = 666;
console.log(Person.num);
</script>
</head>
<body>
</body>
</html>

在企业开发中通过构造函数调用的方法,我们就称之为 ​​静态方法​

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Demo</title>
<script type="text/javascript">
function Person() {
this.name = "BNTang";
this.say = function () {
console.log("hello world");
}
}

Person.run = function () {
console.log("run");
}
Person.run();
</script>
</head>
<body>
</body>
</html>
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 12天前 0

暂无评论

推荐阅读
TEZNKK3IfmPf