class Start {
constructor(uname, age) {
this.uname = uname;
this.age = age;
}
sing() {
console.log("我会唱歌");
}
}
var ldh = new Start("刘德华", 20);
ldh.sing();
console.log(ldh.age);
1、通过关键字Class 定义一个Start类
2、实例对象是通过new Start()
3、Class是生产对象的一个模板,抽取了对象的公共属性。
在es5之前还没有class关键字,是通过构造函数来实例一个对象。
function Start(uname, age) {
this.uname = name;
this.age = age;
this.sing = function() {
console.log("我会唱歌");
}
}
var ldh = new Start("刘德华", 22);
ldh.sing();
var ldh = new Start("刘德华", 22);
var zxy = new Start("张学友", 29);
console.log(ldh.sing === zxy.sing);
Start.prototype.dance = function() {
console.log("我会跳舞");
}
var ldh = new Start("刘德华", 22);
var zxy = new Start("张学友", 29);
//console.log(ldh.sing === zxy.sing);
console.log(ldh.dance === zxy.dance);
console.log(Start.prototype);
console.log(ldh.proto);
Start.prototype = {
dance: function() {
console.log(“我会跳舞”);
}
}
console.log(Start.prototype);
由图片可见,如果是通过字面量的形式,会将Start的构造函数给覆盖,有的时候会通过手动调用的方法,重新将构造函数写进对象里面。
1、实例变量是指在构造函数中,通过this所指向的变量。
2、静态变量是通过类名指向的变量。
1、在类中,this所指向的是实例对象。
2、在成员方法中,this所指向的是调用它,可能是变量也可能是对象。
本文地址:https://blog.csdn.net/qq_44869984/article/details/107250977
如对本文有疑问, 点击进行留言回复!!
同事牛逼啊,写了个隐藏 bug,我排查了 3 天才解决问题!
【JavaScript笔记(一)】万丈高楼平地起 - 基本概念篇
轻松解决 org.apache.taglibs.standard.tlv.JstlCoreTLV 困惑
网友评论