学习JavaScript高级第九天(面向对象、原型、原型链)
function sum(x
, y
) {
let total
= x
+ y
;
this.total
= total
;
console
.log(this)
return total
;
}
let res
= new sum(10,20);
console
.log(res
);
function Fn(x
, y
) {
let str
= "自定义类";
this.total
= x
+ y
;
this.say = function(){
console
.log('OK');
};
}
let f1
= new Fn(10,20);
console
.log(f1
.total
);
console
.log(f1
.say
);
console
.log(f1
.str
);
let f2
= new Fn;
console
.log(f1
=== f2
);
function Fn(){
this.x
= 100;
this.y
= 200;
this.getX = function(){
console
.log(this.x
);
}
}
Fn
.prototype
.getX = function(){
console
.log(this.x
);
}
Fn
.prototype
.getY = function(){
console
.log(this.y
);
};
let f1
= new Fn;
let f2
= new Fn;
console
.log(f1
.getX
=== f2
.getX
);
console
.log(f1
.getY
=== f2
.getY
);
console
.log(f1
.__proto__
.getY
=== Fn
.prototype
.getY
);
console
.log(f1
.__proto__
.getX
=== f2
.getX
);
console
.log(f1
.getX
=== Fn
.prototype
.getX
);
console
.log(f1
.constructor
);
console
.log(Fn
.prototype
.__proto__
.constructor
);
f1
.getX();
f1
.__proto__
.getX();
f2
.getY();
Fn
.prototype
.getY();
function fun() {
this.a
= 0;
this.b = function(){
alert(this.a
);
}
}
fun
.prototype
= {
b
: function(){
this.a
= 20;
alert(this.a
);
},
c
: function(){
this.a
= 30;
alert(this.a
);
}
}
var my_fun
= new fun();
my_fun
.b();
my_fun
.c();
转载请注明原文地址: https://lol.8miu.com/read-20639.html