Jump to content

Rod

Members
  • Posts

    8
  • Joined

  • Last visited

Posts posted by Rod

  1. well.... using that (your) logic i might as well put 70 in "theResult" in the first place.

    I'm not trying to do something in particular with this, i was just wondering "can it be done", and so....now knowing that it can be done, it might come in useful in the future. (not necessarily on a math problem) . I don't doubt your comment that "eval" is inefficient"......i have no idea either way, I'm new to javascript, but... it's available and it worked.

  2. Just in case someone is interested, I managed to get it to work. I've tried passing the "+" without the quotes and then doing toString() on the receiving parameter but that did not work but as it in now, it does work.

    function doMath(a,b,doThis){
        return    eval(a.toString().concat(doThis, b.toString()));
    }

    var theResult = doMath(50, 20, "+");
    alert(theResult);

  3. Is there a way of passing arithmetic operators via a functions' parameters and/or arguments. e.g

    ##########################################################################
    function doTheMath(a, b, doThis) {
        return a  doThis  b;    //  have this evaluate to... a + b or a * b ...etc as appropriate.
    }

    var theResult = doTheMath(4,10, + );     // the "+" could be any of the arithmetic operators.
    ##########################################################################

    I know it's possible to do it with multiple "if" statements or "switch" statement or callbacks (yes..i watched a video on callbacks on...."a well known video sharing website",  but is there an easier (smaller code) way of doing it.

    Just curious....I'm a newbe.

     

  4. Quote from Parent website

    "Prototype Properties"

    "JavaScript objects inherit the properties of their prototype.

    The delete keyword does not delete inherited properties, but if you delete a prototype property, it will affect all objects inherited from the prototype."

    so why does the script below work?

    <script>
    function Person(first, last, age, eye) {
        this.firstName = first;
        this.lastName = last;
        this.age = age;
        this.eyeColor = eye;
    }
    var myMother = new Person("Sally", "Rally", 48, "green");

    delete myMother.age; // apparently... this should not work as "age" (as I understand) has been inherited from the Person prototype.

    document.getElementById("demo").innerHTML =
    "My father is " + myFather.age + ". My mother is " + myMother.age; 
    </script>
    myMothe.age  now outputs undefined and that's what i expected.

    What am i misunderstanding?

    Thank you in advance.

×
×
  • Create New...