Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7567831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:46:01+00:00 2026-05-30T14:46:01+00:00

I have a class in javascript which define the toString method,however when I want

  • 0

I have a class in javascript which define the toString method,however when I want to print it in the page,it always print [object object] in IE(6-8).

But it works in firefox or chrome(they all print ‘kk’ in the example below).

I wonder why?

This is the example code:

function Person(name){
  this.name=name;
}
Person.prototype.toString=function(){
  return this.name;
}
var p=new Person('kk');
document.getElementById('dis').innerHTML=p.toString();

What is the problem?


BTW,this is the code in my application:

function inherit(pro) {
    function F() {};
    F.prototype = pro;
    return new F();
}
var Class = function() {
    var clazz = null,
    pros = {}; // root of chain
    for (var i = 0; i < arguments.length; i++) {
        var arg = arguments[i];

        if (typeof arg === "function") {
            arg = arg.prototype;
        } else {
            if (arg.init) {
                clazz = arg.init;
                delete arg.init;
            }
            var o = arg;
            arg = (function() {
                function F() {};
                F.prototype = pros;
                return new F;
            })();
            for (var key in o) arg[key] = o[key];
        }

        pros = arg;
    }

    clazz.prototype = pros;
    return clazz;
};

var Person = Class({
    init: function(name) {
        this.name = name;
    },
    toString: function() {
        return this.name;
    }
});

function init() {
    var p = new Person('kk');
    document.getElementById('dis').innerHTML = p.toString();
}
window.onload = init;

Screen shot:

IE

Firefox

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T14:46:02+00:00Added an answer on May 30, 2026 at 2:46 pm

    Ok I see your issue now.

    In all older version of IE (previous to 9) the javascript engine does not let you modify an element’s prototype functions.

    So the default toString() of an object is [object Object]

    You might have to think of a different approach to your code for older versions of IE.

    See article here: http://blog.motane.lu/2007/09/20/elementprototype-in-ie/


    Final answer from comments below:

    .toString is already a predefined function in the prototype of all objects, and it cannot be overridden in IE. Try using a different function name.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this line of Javascript which runs a function I sourced online. However
I have a very simple Javascript (jQuery) code, which just add a class name
I have a JavaScript class that I have made and put it into its
Is there a best-practice or common way in JavaScript to have class members as
I have some javascript that goes out and fetches a javascript class on another
I have a class that stores paths to CSS and Javascript files in arrays.
I just came across an interesting situation in JavaScript. I have a class with
I have a javascript function (class) that takes a function reference as one paremter.
I have a small class that displays an alert via Javascript. I am using
I have in my javascript these 2 functions classes: // product class function Product()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.