// 利用Object构造函数
let obj1 = new Object()
// 利用字面量
let obj2 = {}
// 利用自定义构造函数
function Star (uname, age) {
this.uname = uname
this.age = age
this.sing = function () {
console.log(`${this.uname}会唱歌`)
}
}
let ldh = new Star('刘德华', 18)
let zxy = new Star('张学友', 29)
ldh.sing()
用构造函数new一个对象时,会在内存为其分配空间,然后令this指向这个对象,然后执行内部代码生成属性和方法,然后返回这个对象
实例成员就是构造函数内部通过this添加的成员 如上uname, age, sing就是实例成员
实例成员必须通过实例对象来访问和使用
console.log(Star.uname); 这样直接通过构造函数访问实例成员会报错,值为undefined
Star.sex = ‘男’
// 如上直接给构造函数添加的成员称为静态成员 sex就是一个静态成员
// 静态成员只能直接通过构造函数访问 且不能通过实例对象访问
console.log(Star.sex);
function Star (uname, age) {
this.uname = uname
this.age = age
this.sing = function () {
console.log(`${this.uname}会唱歌`);
}
}
let ldh = new Star('ldh', 18)
let zxy = new Star('zxy', 18)
console.log(ldh.sing === zxy.sing);
如上所示,两个对象的相同方法并不严格相等,说明其在内存中各自占有不同空间
function Star (uname, age) {
this.uname = uname
this.age = age
}
Star.prototype.sing = function () {
console.log(`我会唱歌`);
}
let ldh = new Star('ldh', 18)
let zxy = new Star('zxy', 18)
ldh.sing()
zxy.sing()
以上代码块即利用原型对象来定义方法
function Star (uname, age) {
this.uname = uname
this.age = age
}
Star.prototype.sing = function () {
console.log(`我会唱歌`);
}
let ldh = new Star('ldh', 18)
let zxy = new Star('zxy', 18)
console.log(ldh.__proto__ === Star.prototype);
function Star (uname, age) {
this.uname = uname
this.age = age
}
Star.prototype.sing = function () {
console.log(`我会唱歌`);
}
let ldh = new Star('ldh', 18)
let zxy = new Star('zxy', 18)
console.log(Star.prototype.constructor)
console.log(ldh.__proto__.constructor);
如上代码块所示,constructor属性指向构造函数本身,他的主要作用在于记录对象引用于哪个构造函数,他可以让原型对象重新指向原来的构造函数
function Star (uname, age) {
this.uname = uname
this.age = age
}
Star.prototype = {
constructor: Star,
sing: function() {console.log(`我会唱歌`);},
movie: function() {console.log(`我会演戏`);}
}
let ldh = new Star('ldh', 18)
let zxy = new Star('zxy', 18)
console.log(Star.prototype.constructor)
console.log(ldh.__proto__.constructor);
function Star (uname) {
this.uname = uname
}
Star.prototype.sing = function () {
console.log('我会唱歌');
}
let ldh = new Star('ldh')
console.log(Star.prototype.__proto__ === Object.prototype);
如上图,Object原型对象的__proto__属性指向一个空对象,而自定义构造函数的原型对象的__proto__属性指向的是Object构造函数的原型对象。
Array.prototype.sum = function () {
let sum = 0
for (let d of this) {
sum += d
}
return sum
}
arr = [1,2,3,4,5,6]
console.log(arr.sum());
let obj = {
uname: 'zhaoji'
}
function fn () {
console.log(this.uname);
}
fn.call(obj)
function Father (uname, age) {
this.uname = uname
this.age = age
}
function Son (uname, age) {
Father.call(this, uname, age)
}
let son = new Son('刘德华', 18)
console.log(son);
function Father () {
}
Father.prototype.money = function () {
console.log(10000);
}
function Son () {
Father.call(this)
}
Son.prototype = new Father()
Son.prototype.constructor = Son
let son = new Son()
son.money()
这样借用父构造函数的实例对象来继承父构造函数的方法时,对子实例对象单独定义新方法的话其实是在该实例对象上增加方法,并不会影响父构造函数的原型对象
class Star {
}
console.log(typeof Star);
class Star {
constructor (uname) {
this.uname = uname
}
sing () {
console.log('小苹果');
}
}
let ldh = new Star('刘德华')
class Father {
constructor () {
}
money () {
console.log(100);
}
}
class Son extends Father {
constructor (x, y) {
super(x, y)
}
}
let son = new Son()
son.money()