mapwith.js revision 1336:efb5f54092ed
160484Sobrien/*
278828Sobrien * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
378828Sobrien *
478828Sobrien * Redistribution and use in source and binary forms, with or without
560484Sobrien * modification, are permitted provided that the following conditions
678828Sobrien * are met:
778828Sobrien *
878828Sobrien *   - Redistributions of source code must retain the above copyright
978828Sobrien *     notice, this list of conditions and the following disclaimer.
1078828Sobrien *
1178828Sobrien *   - Redistributions in binary form must reproduce the above copyright
1278828Sobrien *     notice, this list of conditions and the following disclaimer in the
1378828Sobrien *     documentation and/or other materials provided with the distribution.
1478828Sobrien *
1578828Sobrien *   - Neither the name of Oracle nor the names of its
1678828Sobrien *     contributors may be used to endorse or promote products derived
1778828Sobrien *     from this software without specific prior written permission.
18218822Sdim *
1978828Sobrien * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
2078828Sobrien * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
2189857Sobrien * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2289857Sobrien * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
2389857Sobrien * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
2478828Sobrien * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2560484Sobrien * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
2689857Sobrien * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
2789857Sobrien * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
2889857Sobrien * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2989857Sobrien * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3089857Sobrien */
3160484Sobrien
3260484Sobrien// Using a Java map with Javascript "with" statement
3360484Sobrien
3460484Sobrienvar map = new java.util.HashMap();
3560484Sobrienmap.put("foo", 34);
3660484Sobrienmap.put("bar", "hello");
3760484Sobrien
3860484Sobrienvar obj = {
3960484Sobrien    __noSuchProperty__: function(name) {
4060484Sobrien        return map.get(name);
4160484Sobrien    }
4260484Sobrien};
4360484Sobrien
4460484Sobrienwith(obj) {
4560484Sobrien   print(foo);
4660484Sobrien   print(bar);
4789857Sobrien}
4860484Sobrien