let_const_closure.js revision 1009:884a8ffb6038
1/*
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/**
25 * JDK-8057678: Tests for let&const keywords in Nashorn
26 *
27 * @test
28 * @run
29 * @option --language=es6
30 * @option -scripting
31 */
32
33function tryIt(code) {
34    try {
35        eval(code)
36    } catch (e) {
37        print(e)
38    }
39}
40
41
42tryIt(<<CODE
43    function a () {
44        this.val = 41
45        let self = this
46        this.b = function () {
47            return self.val;
48        }
49    }
50    c = new a()
51    print(c.b.call(null))
52CODE)
53
54
55tryIt(<<CODE
56        function a () {
57            this.val = 42
58            let self = this
59            this.b = function () {
60                return this.val;
61            }.bind(self)
62        }
63        c = new a()
64        print(c.b.call(null))
65CODE)
66
67tryIt(<<CODE
68    function a () {
69        this.val = 43
70        const self = this
71        this.b = function () {
72            return self.val;
73        }
74    }
75    c = new a()
76    print(c.b.call(null))
77CODE)
78
79tryIt(<<CODE
80        function a () {
81            this.val = 44
82            const self = this
83            this.b = function () {
84                return this.val;
85            }.bind(self)
86        }
87        c = new a()
88        print(c.b.call(null))
89CODE)
90
91tryIt(<<CODE
92       let a = {name : 'test'}
93       let f = function () {
94            print(this.name)
95       }
96       let nf = f.bind(a)
97       nf()
98       if (true) {
99            let a = null
100            nf()
101       }
102       nf()
103CODE)
104
105
106tryIt(<<CODE
107       let arr = []
108       for (let i = 0; i < 3; i++) {
109           arr[i] = function(){return i;}
110       }
111       for (let i in arr) {
112            print(arr[i]())
113       }
114       arr = []
115       for (var i = 0; i < 3; i++) {
116            (function(i){
117                arr[i] = function(){return i;}
118            })(i)
119       }
120       for (let i in arr) {
121           print(arr[i]())
122       }
123CODE)