I was trying your _log function.

Do I need to include a JSON library for this?

When I put your code in a page with nothing else, I see there is a JSON object there with a stringify function.

I just get an empty object logged though.

Can you explain please?

Thanks

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <script>
        function Animal()
        {
            this.breathe = function ()
            {
                alert("I am breathing");
            };

            this.eat = function ()
            {
                alert("I am eating");
            };

            this.sleep = function ()
            {
                alert("I am sleeping");
            };
        }

        var _log = function (label, obj)
        {
            if (typeof console.log === 'function')
            {
                console.log(label + ": " + JSON.stringify(obj, undefined, '\t'));
            }
        };

        var cat = new Animal();

        _log("Animal", cat);

    </script>
</body>
</html>

Just realised that if I add some properties then I am getting the state of my object output. See codebelow. It is not telling me anything about fucntions or prototypes thouhg. Is this what you would expect?

<script>
    function Animal(name, age)
    {
        this.breathe = function ()
        {
            alert("I am breathing");
        };

        this.eat = function ()
        {
            alert("I am eating");
        };

        this.sleep = function ()
        {
            alert("I am sleeping");
        };

        this.name = name;
        this.age = age;
    }

    var _log = function (label, obj)
    {
        if (typeof console.log === 'function')
        {
            console.log(label + ": " + JSON.stringify(obj, undefined, '\t'));
        }

        document.write(label + ": " + JSON.stringify(obj, undefined, '\t'));
    };

    var cat = new Animal("Joey", 14);

    _log("Animal", cat);

</script>

Yeah Dave, sorry for not letting it clear. JSON.stringify is ment to serialize only data properties. I use it a lot for my AJAX requests and results.

But it's quite simple to log methods also:

function Animal(name, age)
    {
        this.breathe = function ()
        {
            alert("I am breathing");
        };
        this.eat = function ()
        {
            alert("I am eating");
        };
        this.sleep = function ()
        {
            alert("I am sleeping");
        };
        this.name = name;
        this.age = age;
    }
    var _log = function (label, obj, logFunctions)
    {
        var _ldata = logFunctions === true ? {
                data: obj,
                methods: []
            } : obj;

        if ( logFunctions === true) {
            for(var j in _ldata.data) {
                if ( typeof _ldata.data[j] === 'function') {
                    _ldata.methods.push({
                        name: j,
                        execution: _ldata.data[j].toString()
                    });
                }
            }
        }
        if (typeof console.log === 'function')
        {
            console.log(label + ": " + JSON.stringify(_ldata, undefined, '\t'));
        }
        document.write(label + ": " + JSON.stringify(_ldata, undefined, '\t'));
    };
    var _logF = function(label, obj) { return _log(label, cat, true); };
    var cat = new Animal("Joey", 14);

    _log("Animal", cat);
    _logF("Animal", cat);

Thanks AleMonteiro, that's very useful.

Finally had a good chance to look at your earlier post which ypo commented as:

"Troy, I may be wrong, but I think Dave was only trying to wrap all of Cat's code inside the Cat function.

And there's a way of doing something similar to it:"

I like your code as it is well encapsulated, thanks!

Glad you like it ^^

I had a few JS pages for testing performance of loops and closures that I'd like to share... but I can't find them! =x I'm not that well organized.

I keep all my stuff in source control that way its always backed up in the cloud - a good habit to get into.

AleMonteiro says:
"As I understand it, the purpose of the closure when defining the object is just about code organization and keeping some desired functions hidden from the global context."

Thanks for revealing that this thread is not about prototypal inheritance -nevermind its Title: "Prototypal Inheritance" - but something completely different, namely: cosures.

My wrong I guess. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.