//Define a functional object to hold persons in JavaScript var Person = function(name) { this.name = name; }; //Add dynamically to the already defined object a new getter Person.prototype.getName = function() { return this.name; }; //Create a new object of type Person var john = new Person("John"); //Try the getter alert(john.getName()); //If now I modify person, also John gets the updates Person.prototype.sayMyName = function() { alert('Hello, my name is ' + this.getName()); }; //Call the new method on john john.sayMyName();
1. Person person; 2. person = new Person("Tom"); 3. changeName(person); 4. 5. //I didn't use Person person below as an argument to be nice 6. static void changeName(Person anotherReferenceToTheSamePersonObject) { 7. anotherReferenceToTheSamePersonObject.setName("Jerry"); 8. }
// Stub the prototype's function so that there is a spy on any new instance // of Sensor that is created. Kind of overkill. sinon.stub(Sensor.prototype, "sample_pressure").returns(0); var sensor = new Sensor(); console.log(sensor.sample_pressure());
public class Person { private String name; private Integer age; } public class PersonServiceImpl implements PersonService { public void changeAge(Integer personId, Integer newAge) { Person person = em.getReference(Person.class, personId); // person is a proxy person.setAge(newAge); } }
// Define a class like this function Person(name, gender){ // Add object properties like this this.name = name; this.gender = gender; } // Add methods like this. All Person objects will be able to invoke this Person.prototype.speak = function(){ alert("Howdy, my name is" + this.name); }; // Instantiate new objects with 'new' var person = new Person("Bob", "M"); // Invoke methods like this person.speak(); // alerts "Howdy, my name is Bob"
// Stub the function on a single instance of 'Sensor'. var sensor = new Sensor(); sinon.stub(sensor, "sample_pressure").returns(0); console.log(sensor.sample_pressure());
var Person = function (name) { this.name = name; }; Person.prototype.getName = function () { return this.name; }; var john = new Person("John"); alert(john.getName()); Person.prototype.sayMyName = function () { alert('Hello, my name is ' + this.getName()); }; john.sayMyName(); var Customer = function (name) { this.name = name; }; Customer.prototype = new Person(); var myCustomer = new Customer('Dream Inc.'); myCustomer.sayMyName(); Customer.prototype.setAmountDue = function (amountDue) { this.amountDue = amountDue; }; Customer.prototype.getAmountDue = function () { return this.amountDue; }; myCustomer.setAmountDue(2000); alert(myCustomer.getAmountDue());
@ModelAttribute("person") public Person getPerson(){ return new Person(); }
var Person = function(name){ this.name = name; }; Person.prototype.walk=function(){ this.step().step().step(); }; var bob = new Person("Bob");
function Person(name){ this.name = name; } Person.prototype.getName = function(){ console.log(this.name); } var person = new Person("George");
Person firstPerson, secondPerson, thirdPerson; firstPerson = secondPerson = thirdPerson = new Person();
@Bean("person") @Before public Person createPerson(){ return new Person(); } @RequestMapping(...) public xxx handlePersonRequest( (@Autowired @Qualifier("person") | @Bean("person")) Person person, xxx){ ... }
var Person = function (age) { this.age = age; }; Person.prototype.isAdult = function () { return this.age > 17; }; var john = new Person(24);