<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../其他/jq.js"></script>
</head>
<body>
<script>
//混入式继承
function Dog(name, color) {
this.name = name;
this.color = color;
}
Dog.prototype.eat = function(x) {
console.log(this.name + "吃了" + x);
}
var ni = new Dog("陪陪", "黑色");
ni.eat("屎");
var ta = {};
for (const key in ni) {
if (ni.hasOwnProperty(key)) {
ta[key] = ni[key];
}
}
console.log(ta);
//我们只拿到了属性,方法都没有啊,而且还只搞了一个,这也叫继承????
//这种继承还像那么回事儿!
//即可以拿到方法,又可以拿到属性
//这叫替换原型继承
function Y1() {
}
var y1 = new Y1();
Y1.prototype = Dog.prototype;
Y1 = Dog.prototype.constructor;
var huu = new Y1("mo", "mo");
console.log(huu);
huu.eat("mi");
//混合式,也就是瞎搞,拿了一堆属性而已
function Y2() {
for (var key in ni) {
if (ni.hasOwnProperty(key)) {
this[key] = ni[key];
}
}
}
var y2 = new Y2();
console.log(y2);
//借助构造函数,没啥用
function Y3(name, color) {
Dog.prototype.constructor.call(this, name, color);
}
var y3 = new Y3("mo", "ko"); //call借代码,不要个脸
console.log(y3);
</script>
</body>
</html>
js基础之继承的实现
猜你喜欢
转载自blog.csdn.net/jvhbi/article/details/106862182
今日推荐
周排行