Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Define an object with some properties and a method:
- var clientData = {
- id: 094545,
- newId: function() {
- var id = "";
- for (var idx in String(this.id)) {
- id += String(Math.floor(Math.random()*10));
- }
- this.id = Number(id);
- },
- fullName: "Not Set",
- // setUserName is a method on the clientData object
- setUserName: function(firstName, lastName) {
- // this refers to the fullName property in this object
- this.fullName = firstName+" "+lastName;
- }
- }
- function getUserInput(firstName, lastName, callback, callbackObj) {
- // Do otherstuff tovalidate name here
- // The use of the Apply function below will set the this object to be callbackObj
- callback.apply(callbackObj, [firstName, lastName]);
- };
- // Pass the clientData.setUserName method and the clientData object as parameters.
- // The clientData object will be used by the Apply function to set the this object
- getUserInput("Barack", "Obamanation", clientData.setUserName, clientData);
- // the fullName property on the clientData was correctly set
- console.log(clientData.fullName);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement