在javascript的自定义对象中使用原型
我已经创建了自定义对象,我想在其中添加一个方法。我想把我的值大写。但它给了我[目标],我知道怎么做在javascript的自定义对象中使用原型,javascript,object,Javascript,Object,我已经创建了自定义对象,我想在其中添加一个方法。我想把我的值大写。但它给了我[目标],我知道怎么做 功能检查对象(姓名、职务、工资){ this.name=name; 这个.title=title; 这个。薪水=薪水; } var woo=新的checkObj(“rajora”,“这是一个测试”,2000年); checkObj.prototype.inc=函数(){ 对于(i=0;i当您调用console.log()并向它传递一个类似woo的对象时,它使用woo.toString()获取它的字
功能检查对象(姓名、职务、工资){
this.name=name;
这个.title=title;
这个。薪水=薪水;
}
var woo=新的checkObj(“rajora”,“这是一个测试”,2000年);
checkObj.prototype.inc=函数(){
对于(i=0;i当您调用console.log()
并向它传递一个类似woo
的对象时,它使用woo.toString()
获取它的字符串表示并打印它
woo
从Object.prototype
继承toString()
,默认情况下,prototype将打印您获得的字符串,即[Object Object]
您必须像这样重写toString()
:
checkObj.prototype.toString = function() {
var result = "checkObj {";
for (var prop in this) {
if (this.hasOwnProperty(prop))
result += (prop + " : " + String(this[prop]).toUpperCase() + ", ");
}
result += ("}");
return result;
}
现在,您只需console.log(woo)
,它就会按预期工作。当您调用console.log()
并向它传递一个类似woo
的对象时,它使用woo.toString()
获取它的字符串表示并打印它
woo
从Object.prototype
继承toString()
,默认情况下,prototype将打印您获得的字符串,即[Object Object]
您必须像这样重写toString()
:
checkObj.prototype.toString = function() {
var result = "checkObj {";
for (var prop in this) {
if (this.hasOwnProperty(prop))
result += (prop + " : " + String(this[prop]).toUpperCase() + ", ");
}
result += ("}");
return result;
}
现在,您只需console.log(woo)
,它就会按预期工作。当您调用console.log()
并向它传递一个类似woo
的对象时,它使用woo.toString()
获取它的字符串表示并打印它
woo
从Object.prototype
继承toString()
,默认情况下,prototype将打印您获得的字符串,即[Object Object]
您必须像这样重写toString()
:
checkObj.prototype.toString = function() {
var result = "checkObj {";
for (var prop in this) {
if (this.hasOwnProperty(prop))
result += (prop + " : " + String(this[prop]).toUpperCase() + ", ");
}
result += ("}");
return result;
}
现在,您只需console.log(woo)
,它就会按预期工作。当您调用console.log()
并向它传递一个类似woo
的对象时,它使用woo.toString()
获取它的字符串表示并打印它
woo
从Object.prototype
继承toString()
,默认情况下,prototype将打印您获得的字符串,即[Object Object]
您必须像这样重写toString()
:
checkObj.prototype.toString = function() {
var result = "checkObj {";
for (var prop in this) {
if (this.hasOwnProperty(prop))
result += (prop + " : " + String(this[prop]).toUpperCase() + ", ");
}
result += ("}");
return result;
}
现在您只需console.log(woo)
,它就可以正常工作。您只需像这样更改inc
功能
checkObj.prototype.inc = function() {
for (var key in this) {
if (this.hasOwnProperty(key)) {
if (typeof this[key] === 'string') {
this[key] = this[key].toUpperCase();
}
}
}
};
这给了我以下输出
{ name: 'RAJORA', title: 'THIS IS A TEST', salary: 2000 }
您只需像这样更改inc
函数即可
checkObj.prototype.inc = function() {
for (var key in this) {
if (this.hasOwnProperty(key)) {
if (typeof this[key] === 'string') {
this[key] = this[key].toUpperCase();
}
}
}
};
这给了我以下输出
{ name: 'RAJORA', title: 'THIS IS A TEST', salary: 2000 }
您只需像这样更改inc
函数即可
checkObj.prototype.inc = function() {
for (var key in this) {
if (this.hasOwnProperty(key)) {
if (typeof this[key] === 'string') {
this[key] = this[key].toUpperCase();
}
}
}
};
这给了我以下输出
{ name: 'RAJORA', title: 'THIS IS A TEST', salary: 2000 }
您只需像这样更改inc
函数即可
checkObj.prototype.inc = function() {
for (var key in this) {
if (this.hasOwnProperty(key)) {
if (typeof this[key] === 'string') {
this[key] = this[key].toUpperCase();
}
}
}
};
这给了我以下输出
{ name: 'RAJORA', title: 'THIS IS A TEST', salary: 2000 }
演示
js代码如下:
function checkObj (name,title,salary){
this.name= name;
this.title= title;
this.salary= salary;
}
checkObj.prototype.inc=function(){
var self=this;
for(var i in self){
if(self.hasOwnProperty(i)){
output(i);
}
}
function output(item){
if(typeof self[item]==='string'){
self[item]=self[item].toUpperCase();
console.log(self[item]);
}
}
};
对您有帮助吗?演示
js代码如下:
function checkObj (name,title,salary){
this.name= name;
this.title= title;
this.salary= salary;
}
checkObj.prototype.inc=function(){
var self=this;
for(var i in self){
if(self.hasOwnProperty(i)){
output(i);
}
}
function output(item){
if(typeof self[item]==='string'){
self[item]=self[item].toUpperCase();
console.log(self[item]);
}
}
};
对您有帮助吗?演示
js代码如下:
function checkObj (name,title,salary){
this.name= name;
this.title= title;
this.salary= salary;
}
checkObj.prototype.inc=function(){
var self=this;
for(var i in self){
if(self.hasOwnProperty(i)){
output(i);
}
}
function output(item){
if(typeof self[item]==='string'){
self[item]=self[item].toUpperCase();
console.log(self[item]);
}
}
};
对您有帮助吗?演示
js代码如下:
function checkObj (name,title,salary){
this.name= name;
this.title= title;
this.salary= salary;
}
checkObj.prototype.inc=function(){
var self=this;
for(var i in self){
if(self.hasOwnProperty(i)){
output(i);
}
}
function output(item){
if(typeof self[item]==='string'){
self[item]=self[item].toUpperCase();
console.log(self[item]);
}
}
};
对你有帮助吗?你从console.log(woo)
?woo
这是你的objectcheckObj{name=“RAJORA”,title=“这是一个测试”,薪水=2000,更多…}然后console.log(JSON.stringify(woo))
(在你的情况下)你从console.log(woo)
执行什么{name=“RAJORA”,title=“这是一个测试”,salary=2000,更多…}然后console.log(JSON.stringify(woo))
(在你的例子中)你从console.log(woo)
?woo
这是你的objectcheckObj{name=“RAJORA”,title=“这是一个测试”,salary=2000,更多…}然后console.log(JSON.stringify(woo))
(在您的情况下)您从console.log(woo)
执行什么?woo
这是您的objectcheckObj{name=“RAJORA”,title=“这是一个测试”,salary=2000,更多…}然后console.log(JSON.stringify(woo))
(在您的情况下)谢谢,它给出了期望的结果,但也抛出了错误。再次感谢。有一件事我需要知道什么是prop.Does integerprop@amit我刚刚滚动了答案的最终版本,请选中“编辑”。@amitprop
是一个变量,当它在woo
上循环时,它会保存每个属性的名称。谢谢它给了d我期待结果,但它也抛出了错误。再次感谢。有一件事我需要知道什么是prop.Does integerprop@amit我刚刚滚动了答案的最终版本,请选中“编辑”。@amitprop
是一个变量,当它在woo
上循环时,它会保存每个属性的名称。谢谢,它给出了所需的结果,但是也是抛出错误。再次感谢。有一件事我需要知道什么是prop.Does integerprop@amit我刚刚滚动了答案的最终版本,请选中“编辑”。@amitprop
是一个变量,当它在woo
上循环时,它会保存每个属性的名称。谢谢,它给出了期望的结果,但也抛出了错误r、 再次感谢。有一件事我需要知道什么是prop.Does integerprop@amit我刚刚滚动了答案的最终版本,请选中编辑。@amitprop
是一个变量,当它在woo
上循环时,它会保存每个属性的名称。