0

Using javascript console of Google Chrome Developer tool, for example, it will be possible to inspect global object like Math.

You can simple write Math in your console and then return. You will be able to access all properties like E, PI and methods like abs, ceil, etc.

Also String is a global object like Math, but if you try the same operation, the javascript condole output will be different and you will be not able to see Properties and Methods.

In order to see Properties and Methods of String you need to make something like this.

var c = new String("asd")

And then inspect the c objet.

So my question is, can you make an example of very simple objects implemented as String and then as Math?

1
  • 1
    Type String.prototype in your console and hit Enter. Commented Sep 12, 2011 at 21:00

2 Answers 2

1

In regular OO Math would be seen as a Static class, there is only one instance of it. String would be a normal class, where you create more then one instance.

As for javascript; you can make an object with values as functions:

var Calc = {
 Addition : function(a,b)
 {
   return a+b;
 }
}

You can call Calc.Addition(1,2) and get 3 back.

In the other sample, where you have to instantiate the object first you could have something like this:

var Person = function(name)
{
 this.Name = name;

 this.Walk = function()
 {
   console.log(this.Name + "  is walking");
 }
}

var p = new Person("AntonJS");

In your console log you'd see p has a property called Name, and a function called Walk.

Sign up to request clarification or add additional context in comments.

2 Comments

I hope this is what you ment, it was kinda difficult to understand. Perhaps you should take a more in depth look into OO.
(In the samples i assume the objects are declared within the same scope etc)
1

the diference is the native types:

typeof(new String("asd")) // 'object'
typeof("asd") // 'string' 

but

(new String("asd")).constructor.name // String
"asd".constructor.name // String

The properties and methods for String object are implemented by the standard of the ECMA-262 (The Javascript Standard) and extended by the current javascript engine. you could see as above says in String.prototype object definition of String object.

EDIT: Now i catch your question better. Math is an abstract object, "asdf" is an instance of string.

var MockMath = {
    'sin' : function(){
    }
    ,'cos' : function(){
    }
    /* ... */
};

var MockString = function() {
    /* ... */
};

MockString.prototype = {
    'split'  : function() {
    }
    ,'toLowerCase' : function() {
    }
    ,'toUpperCase' : function() {
    }
};

console.debug(new MockString("asdf"));
console.debug(MockMath);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.