Rod
-
Posts
8 -
Joined
-
Last visited
Content Type
Profiles
Forums
Events
Posts posted by Rod
-
-
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.
-
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); -
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.
-
Ok... thanks.... I'll have a look on google and youtube. Thank you for your time anyway.
-
There no real reason, just trying to understand what I've learnt and might be good to know for the future..
I assumed that because "age" property is in the "Person" object prototype, then the myMother object would "inherit" the "age" property. So... with reference to your last comment, I must be misunderstanding the "inherit" thing.
-
The script above was copied from this website, under the heading "creating a prototype" and in there is says to use "object constructor function" which is what I have done.
The link you gave shows how to create new methods to pre-existing javascript object. (in your case..... an Array).
-
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.
The delete keyword does not delete inherited properties,...but i just have!!
in JavaScript
Posted
Just watched a video on youtube on inheritance by Nathan Wall (might be of interest to someone who struggling like me) and together with the replies I've had here......, I get the inheritance thing now. Thanks to all that have replied to this post.