使用 Javascript 原型在对象中添加函数
Adding function in object using Javascript prototype
我是 Javascript 的新手 我正在尝试在 Object.prototype 创建的对象原型中添加一些函数 我试过这段代码
var a=function(){this.k="yes";}
a.prototype.b1=function(){console.log("function of a");};
var b=Object.create(a.prototype);
b.prototype.c1=function(){console.log("function of b");};
b.c1();
它给了我错误“无法设置未定义的 属性 'c1'”,我不知道我在哪里做错了,请指导我。提前致谢
你的代码应该是这样的:
var a=function(){this.k="yes";};
a.prototype.b1=function(){console.log("function of a");};
var b =function(){};
b.prototype=new a();
b.prototype.c1=function(){console.log("function of b");};
var bObj = new b();
bObj.c1()
你试图在这里实现两个不同的目标。
第一个 :
var b=Object.create(a.prototype);
我假设您正在尝试扩展 b 中的 a
class。考虑在创建后直接修改 b 原型:
//Create b class
var b = function(){this.key = 2};
//Extends a in b
var b.prototype = new a();
第二 :
b.prototype.c1=function(){console.log("function of b");};
b.c1();
您正在尝试使用 b.c1();
从您的 class 调用您的函数。尝试先在另一个变量 var bObject = new b();
中实例化它,然后调用分配给原型的函数:bObject.c1()
您的整体代码应如下所示:
//Create a class here
var a=function(){this.k="yes";};
//assign b1 function to a class
a.prototype.b1=function(){console.log("function of a");};
//Create b class
var b = function(){this.key = 2};
//extends a in b
b.prototype = new a();
//create c1 function in b class
b.prototype.c1=function(){console.log("function of b");};
//create bObj from b class
var bObj = new b();
//call c1 function defined in b class
bObj.c1();
//can also call b1 function from a class
bObj.b1();
我不确定你到底想做什么,但目前你的问题是 b
是一个普通对象(它继承自 a.prototype
具有 .b1
和.constructor
个属性),没有 b.prototype
属性。尽管如此,你还是试图在那个不存在的东西上设置一个 属性。
您正在寻找
var a = {
b1: function(){console.log("function of a");}
};
var b = Object.create(a);
b.c1 = function(){console.log("function of b");};
b.c1();
b.b1();
不涉及构造函数或 .prototype
属性 - 只是简单的原型继承 - 或者你正在寻找
function A() { this.k="yes"; }
A.prototype.b1 = function(){console.log("function of A.prototype");};
function B() { A.call(this); }
B.prototype = Object.create(a.prototype);
B.prototype.c1 = function(){console.log("function of B.prototype");};
var b = new B();
b.c1();
b.b1();
这是 "class" 结构之间继承的典型示例,即带有伴随原型对象的构造函数。您忘记创建 B
函数并在调用方法之前实例化它。
我是 Javascript 的新手 我正在尝试在 Object.prototype 创建的对象原型中添加一些函数 我试过这段代码
var a=function(){this.k="yes";}
a.prototype.b1=function(){console.log("function of a");};
var b=Object.create(a.prototype);
b.prototype.c1=function(){console.log("function of b");};
b.c1();
它给了我错误“无法设置未定义的 属性 'c1'”,我不知道我在哪里做错了,请指导我。提前致谢
你的代码应该是这样的:
var a=function(){this.k="yes";};
a.prototype.b1=function(){console.log("function of a");};
var b =function(){};
b.prototype=new a();
b.prototype.c1=function(){console.log("function of b");};
var bObj = new b();
bObj.c1()
你试图在这里实现两个不同的目标。
第一个 :
var b=Object.create(a.prototype);
我假设您正在尝试扩展 b 中的 a
class。考虑在创建后直接修改 b 原型:
//Create b class
var b = function(){this.key = 2};
//Extends a in b
var b.prototype = new a();
第二 :
b.prototype.c1=function(){console.log("function of b");};
b.c1();
您正在尝试使用 b.c1();
从您的 class 调用您的函数。尝试先在另一个变量 var bObject = new b();
中实例化它,然后调用分配给原型的函数:bObject.c1()
您的整体代码应如下所示:
//Create a class here
var a=function(){this.k="yes";};
//assign b1 function to a class
a.prototype.b1=function(){console.log("function of a");};
//Create b class
var b = function(){this.key = 2};
//extends a in b
b.prototype = new a();
//create c1 function in b class
b.prototype.c1=function(){console.log("function of b");};
//create bObj from b class
var bObj = new b();
//call c1 function defined in b class
bObj.c1();
//can also call b1 function from a class
bObj.b1();
我不确定你到底想做什么,但目前你的问题是 b
是一个普通对象(它继承自 a.prototype
具有 .b1
和.constructor
个属性),没有 b.prototype
属性。尽管如此,你还是试图在那个不存在的东西上设置一个 属性。
您正在寻找
var a = {
b1: function(){console.log("function of a");}
};
var b = Object.create(a);
b.c1 = function(){console.log("function of b");};
b.c1();
b.b1();
不涉及构造函数或 .prototype
属性 - 只是简单的原型继承 - 或者你正在寻找
function A() { this.k="yes"; }
A.prototype.b1 = function(){console.log("function of A.prototype");};
function B() { A.call(this); }
B.prototype = Object.create(a.prototype);
B.prototype.c1 = function(){console.log("function of B.prototype");};
var b = new B();
b.c1();
b.b1();
这是 "class" 结构之间继承的典型示例,即带有伴随原型对象的构造函数。您忘记创建 B
函数并在调用方法之前实例化它。