JS9.编写inherit()函数,它返回了一个继承自原型对象p的属性的新对象,这里使用ECMAScript 5中的Object.create()函数(如果存在的话),如果不存在Object.create(),则退化使用其他方法
时间: 2024-05-09 19:14:30 浏览: 12
可以这样实现:
```javascript
function inherit(p) {
if (p == null) throw TypeError(); // p不能为null或undefined
// 如果Object.create()存在,则使用它
if (Object.create) {
return Object.create(p);
}
// 否则,退化使用其他方法
var t = typeof p;
if (t !== "object" && t !== "function") throw TypeError();
function f() {}
f.prototype = p;
return new f();
}
```
使用示例:
```javascript
var o = { x: 1 };
var p = inherit(o);
p.y = 2;
console.log(p.x); // 1
console.log(p.y); // 2
console.log(o.hasOwnProperty("y")); // false,没有继承自p的属性
```
相关问题
JS 编写inherit()函数,它返回了一个继承自原型对象p的属性的新对象,这里使用ECMAScript 5中的Object.create()函数(如果存在的话),如果不存在Object.create(),则退化使用其他方法
```javascript
function inherit(p) {
if (p == null) throw TypeError(); // p不能为空
// 如果Object.create()存在,则使用它
if (Object.create) return Object.create(p);
// 否则,使用其他方法
var t = typeof p;
if (t !== "object" && t !== "function") throw TypeError();
function f() {}
f.prototype = p;
return new f();
}
```
该函数先检查传入的参数p是否为空,如果为空则抛出一个类型错误的异常。然后判断当前环境下是否支持Object.create()函数,如果支持则直接使用它来创建一个新对象并继承原型对象p的属性。如果不支持,则先检查p的类型,如果不是对象或函数则抛出一个类型错误的异常,否则创建一个空函数f,并将f的原型对象设置为p,最后使用new操作符来创建一个继承自p的新对象。
写一个函数,接收两个参数,父类SupClass和子类SubClass 表示父构造函数和子构造函数,实现子类继承父类,提示,子类继承父类时不要丢失子类自己的属性和方法
好的,可以使用以下代码实现:
```
function inherit(SupClass, SubClass) {
// 通过一个空函数中转实现原型链的继承
function F() {}
F.prototype = SupClass.prototype;
SubClass.prototype = new F();
SubClass.prototype.constructor = SubClass;
// 记录子类与父类之间的继承关系
SubClass.superClass = SupClass.prototype;
// 子类自己的属性和方法
SubClass.prototype.subProp = "sub property";
SubClass.prototype.subMethod = function() {
console.log("sub method");
};
}
// 父类
function SupClass() {
this.supProp = "sup property";
}
SupClass.prototype.supMethod = function() {
console.log("sup method");
};
// 子类
function SubClass() {
// 调用父类构造函数
SupClass.call(this);
this.subProp = "override sup property";
}
// 继承父类
inherit(SupClass, SubClass);
// 测试
var subObj = new SubClass();
console.log(subObj.supProp);
subObj.supMethod();
console.log(subObj.subProp);
subObj.subMethod();
```
输出结果为:
```
sup property
sup method
override sup property
sub method
```
解释:inherit函数实现了原型链的继承,并且记录了子类与父类之间的继承关系。在子类构造函数中,调用父类构造函数,同时也可以重写父类的属性。最后,创建子类的实例,并且可以访问父类和子类的属性和方法。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)