addition.js revision 6:5a1b0714df0e
1/* 2 * Copyright (c) 2010, 2013, 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 * Checks for binary addition operator. 26 * 27 * @test 28 * @run 29 */ 30 31// number addition 32var x = Math.PI + Math.E; 33print(typeof(x)); 34print(x); 35 36// string concatenation 37x = "hello, " + "world"; 38print(typeof(x)); 39print(x); 40 41// string + number 42x = "E is " + Math.E; 43print(typeof(x)); 44print(x); 45 46// number + string 47x = Math.PI + " is PI"; 48print(typeof(x)); 49print(x); 50 51// number + undefined 52x = Math.E + undefined; 53print(typeof(x)); 54print(x); 55 56x = undefined + Math.PI; 57print(typeof(x)); 58print(x); 59 60// object with "valueOf" method added to number 61var obj = { 62 valueOf: function() { return 44.55; } 63}; 64 65x = 45.66 + obj; 66print(typeof(x)); 67print(x); 68 69x = obj + 3.14; 70print(typeof(x)); 71print(x); 72 73// object with "toString" method added to number 74var obj2 = { 75 toString: function() { return "obj2.toString"; } 76}; 77 78x = "hello, " + obj2; 79print(typeof(x)); 80print(x); 81 82x = obj2 + " hello"; 83print(typeof(x)); 84print(x); 85