Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // create object constructor
- function Foo(prop1, prop2) {
- this.prop1 = prop1;
- this.prop2 = prop2;
- // create object method PrintObjectPropertyM
- // and retreive property value
- // * there is M at the end of property to distinguish
- // method/function name
- // how can I add placeholder/argument in method and then
- // call method on object with provided argument (here prop1 or prop2)
- this.printObjectPropertyM = function() {
- console.log(this.prop1);
- };
- }
- // instantiate new object Bar of Foo type
- var Bar = new Foo("prop1val", "prop2val");
- // create function which print object property and take object as an argument
- var printObjectProperty = function(object) {
- console.log(object.prop1);
- };
- // call printObjectProperty with Bar object as an argument
- printObjectProperty(Bar); // logs prop1var in console
- // call Bar method printObjectPropertyM
- Bar.printObjectPropertyM(); // logs prop1val in console
Advertisement
Add Comment
Please, Sign In to add comment