function Foo() {} var foo = new Foo(); typeof Foo; // == "function" typeof foo; // == "object" foo instanceof Foo; // == true foo.constructor.name; // == "Foo" Foo.name // == "Foo" Foo.prototype.isPrototypeOf(foo); // == true Foo.prototype.bar = function (x) {return x+x;}; foo.bar(21); // == 42
function Foo(){ this.confusing = 'hell yeah'; } var myObject = new Foo();
public Foo acquireFoo(int id) { Foo result = null; if (id > 50) { result = fooService.read(id); } else { result = new Foo(id); } assert result != null; return result; }
function Foo() {} Foo.prototype = { bar: 123 }; var foo = new Foo(); // foo.bar is 123 foo.bar = 456; // foo.bar is now 456 delete foo.bar; // foo.bar is 123 again.
var obj = new Foo(); (obj instanceof Object); // true (obj instanceof Foo); // true
function Foo(b) { this.baz = b; } Foo.prototype.bar = function bar() { console.log(this.baz); }; var f = new Foo("Charlie"); f.bar(); // logs "Charlie"
function Foo() { } // toString override added to prototype of Foo class Foo.prototype.toString = function() { return "[object Foo]"; } var f = new Foo(); alert(f); // popup displays [object Foo]
public static void main(String [] args){ System.out.println("start"); for(int i = 0; i < 1000*1000*1000; ++i){ Foo foo = new Foo(); } System.out.println(Foo.counter); }
public class Main{ public static void main(String[] args){ Foo f = new Foo("f"); changeReference(f); // It won't change the reference! modifyReference(f); // It will modify the object that the reference variable "f" refers to! } public static void changeReference(Foo a){ Foo b = new Foo("b"); a = b; } public static void modifyReference(Foo c){ c.setAttribute("c"); } }
public class FooTest { @Rule public final ExpectedException exception = ExpectedException.none(); @Test public void doStuffThrowsIndexOutOfBoundsException() { Foo foo = new Foo(); exception.expect(IndexOutOfBoundsException.class); foo.doStuff(); } }
Foo f = new Foo(5); Foo.Bar b = f.new Bar(); b.printVal(); // prints 5
List<Foo> list = new ArrayList<Foo>(); Foo tmp = new Foo(); for (int i = 0; i < 3; i++) { tmp.setValue(i); list.add(tmp); }
List<Foo> list = new ArrayList<Foo>(); for (int i = 0; i < 3; i++) { Foo tmp = new Foo(); // <-- fresh instance! tmp.setValue(i); list.add(tmp); }
Foo x = new Foo("the field"); x.setField("a new field"); System.out.println(x.getField()); // prints "a new field"
Foo copyFoo (Foo foo){ Foo f = new Foo(); //for all properties in FOo f.set(foo.get()); return f; }