test/script/basic/es6/let_const_closure.js

changeset 995
33bde22b7740
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/script/basic/es6/let_const_closure.js	Mon Sep 08 15:37:50 2014 +0400
     1.3 @@ -0,0 +1,123 @@
     1.4 +/*
     1.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/**
    1.28 + * JDK-8057678: Tests for let&const keywords in Nashorn
    1.29 + *
    1.30 + * @test
    1.31 + * @run
    1.32 + * @option --language=es6
    1.33 + * @option -scripting
    1.34 + */
    1.35 +
    1.36 +function tryIt(code) {
    1.37 +    try {
    1.38 +        eval(code)
    1.39 +    } catch (e) {
    1.40 +        print(e)
    1.41 +    }
    1.42 +}
    1.43 +
    1.44 +
    1.45 +tryIt(<<CODE
    1.46 +    function a () {
    1.47 +        this.val = 41
    1.48 +        let self = this
    1.49 +        this.b = function () {
    1.50 +            return self.val;
    1.51 +        }
    1.52 +    }
    1.53 +    c = new a()
    1.54 +    print(c.b.call(null))
    1.55 +CODE)
    1.56 +
    1.57 +
    1.58 +tryIt(<<CODE
    1.59 +        function a () {
    1.60 +            this.val = 42
    1.61 +            let self = this
    1.62 +            this.b = function () {
    1.63 +                return this.val;
    1.64 +            }.bind(self)
    1.65 +        }
    1.66 +        c = new a()
    1.67 +        print(c.b.call(null))
    1.68 +CODE)
    1.69 +
    1.70 +tryIt(<<CODE
    1.71 +    function a () {
    1.72 +        this.val = 43
    1.73 +        const self = this
    1.74 +        this.b = function () {
    1.75 +            return self.val;
    1.76 +        }
    1.77 +    }
    1.78 +    c = new a()
    1.79 +    print(c.b.call(null))
    1.80 +CODE)
    1.81 +
    1.82 +tryIt(<<CODE
    1.83 +        function a () {
    1.84 +            this.val = 44
    1.85 +            const self = this
    1.86 +            this.b = function () {
    1.87 +                return this.val;
    1.88 +            }.bind(self)
    1.89 +        }
    1.90 +        c = new a()
    1.91 +        print(c.b.call(null))
    1.92 +CODE)
    1.93 +
    1.94 +tryIt(<<CODE
    1.95 +       let a = {name : 'test'}
    1.96 +       let f = function () {
    1.97 +            print(this.name)
    1.98 +       }
    1.99 +       let nf = f.bind(a)
   1.100 +       nf()
   1.101 +       if (true) {
   1.102 +            let a = null
   1.103 +            nf()
   1.104 +       }
   1.105 +       nf()
   1.106 +CODE)
   1.107 +
   1.108 +
   1.109 +tryIt(<<CODE
   1.110 +       let arr = []
   1.111 +       for (let i = 0; i < 3; i++) {
   1.112 +           arr[i] = function(){return i;}
   1.113 +       }
   1.114 +       for (let i in arr) {
   1.115 +            print(arr[i]())
   1.116 +       }
   1.117 +       arr = []
   1.118 +       for (var i = 0; i < 3; i++) {
   1.119 +            (function(i){
   1.120 +                arr[i] = function(){return i;}
   1.121 +            })(i)
   1.122 +       }
   1.123 +       for (let i in arr) {
   1.124 +           print(arr[i]())
   1.125 +       }
   1.126 +CODE)
   1.127 \ No newline at end of file

mercurial