test/script/basic/es6/let_const_closure.js

Mon, 08 Sep 2014 15:37:50 +0400

author
yan
date
Mon, 08 Sep 2014 15:37:50 +0400
changeset 995
33bde22b7740
permissions
-rw-r--r--

8057678: Tests for let and const keywords in Nashorn
Reviewed-by: hannesw, lagergren
Contributed-by: Sergey Lugovoy <sergey.lugovoy@oracle.com>

     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  */
    24 /**
    25  * JDK-8057678: Tests for let&const keywords in Nashorn
    26  *
    27  * @test
    28  * @run
    29  * @option --language=es6
    30  * @option -scripting
    31  */
    33 function tryIt(code) {
    34     try {
    35         eval(code)
    36     } catch (e) {
    37         print(e)
    38     }
    39 }
    42 tryIt(<<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))
    52 CODE)
    55 tryIt(<<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))
    65 CODE)
    67 tryIt(<<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))
    77 CODE)
    79 tryIt(<<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))
    89 CODE)
    91 tryIt(<<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()
   103 CODE)
   106 tryIt(<<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        }
   123 CODE)

mercurial