Various pitfalls of js and java, and jsjava pitfalls

Source: Internet
Author: User

Various pitfalls of js and java, and jsjava pitfalls

1. js string "="

The String type of js is different from the String type of java. equals is not required for comparison. You can directly use "= ".

After testing, this "=" seems to be a pitfall.

 

<script type="text/javascript">    var a=new String("hehe");    var b=new String("hehe");    var c="hehe";    alert("a==c: "+(a==c));//true    alert("b==c: "+(b==c));//true    alert("a==b: "+(a==b));//false    alert("hehe"=="hehe");//true</script>

How can this be unreasonable? A = c, B = c, but a is not equal to B !!!

My conclusion is that although the String type in js does not have the equals method, when the String type object is compared with another String type variable, the method used is similar to the equals method in java. When the two String types are compared, "=" is similar to "=" in java, that is, to compare whether the two objects are the same object.

The corresponding java code does not have such a tangle

String a=new String("hehe");        String b=new String("hehe");        String c="hehe";        String d="hehe";        System.out.println("a==b: "+(a==b));//false        System.out.println("a==c: "+(a==c));//false        System.out.println("c==b: "+(c==b));//false        System.out.println("c==d: "+(c==d));//true        System.out.println("a.equals(b): "+(a.equals(b)));//true

In java, when the String object created through new is compared with "=", the address value of the object is compared. In js, only when both objects are created using new, compare the address value.

2. Differences between arrays in js and List in java:

Similarities:

1. The types of added elements can be different. (After java uses generics, only the specified element types can be added)

2. Variable Length

Differences:

Js can access the badge that exceeds the length of the array. If the returned value of list. get (index) index in undefined and java exceeds the length of the array, an exception occurs during runtime: java. lang. IndexOutOfBoundsException

        ArrayList list=new ArrayList();        list.add("re");        list.add(2);        System.out.println(list.get(2));//java.lang.IndexOutOfBoundsException
    var t=[0,"a"];    document.write(t[5]);//undefined

3. Functions in js

 

// A js function can be considered as a class. Its Attributes are divided into: // instance attributes: use this as the prefix // class attributes: Use the function name as the prefix // local variable: using var declaration or direct assignment/* different from java, js class attributes can only be accessed through classes, but cannot be accessed using objects * in the same class, you can have class attributes and instance attributes with the same name. */var Person = function (name, age) {var me = "memeda"; this. name = name; this. age = age; Person. gender = "female"; this. gender = "male"; Person. t = "hehe";} var p = new Person ("Lucy", "32"); with (document) {write (p. name + "<br/>"); // lucy write (p. age + "<br/>"); // 32 write (p. gender + "<br/>"); // male write (Person. gender + "<br/>"); // female write (p. t + "<br/>"); // undefined write (p. me + "<br/>"); // undefined}

 

Attributes of js objects can be added at will
The above p object does not have the t and me attributes. After adding:

p.me="ok";p.t="ttok";document.write(p.t+"<br/>");//ttokdocument.write(p.me+"<br/>");//ok

4. Functions in js are independent and do not belong to any class or object;

Although functions in js can be defined in a class, they can be called by objects of other classes. Methods in java are instance methods or static methods (class methods ), is an object or class. Call () method: enables different objects to call a method.

// Call syntax format: function name. call (call object, parameter 1, parameter 2 ·); var wolf = function (name, voice, action) {this. name = name; this. voice = voice; this. action = action; this.info = function () {document. write (this. the cry of name + "is" + this. voice + "the way to eat food is:" + this. action + "<br/>") ;}} new wolf ("wolf", "ao ~ "," Bite "). info (); // the wolf cry is ao ~ The way to eat food is: Bite var cat = function (name, voice, action) {this. name = name; this. voice = voice; this. action = action;} new wolf ("wolf", "ao ~ "," Bite "). info. call (new cat (" cat "," miao ~ "," Selling cute ") // The Call of the cat is miao ~ The way to eat food is: Selling cute

 

5. The parameter transfer of js functions is the same as that of java Methods: both values are passed.

Var Person = function () {this. name = "wang"; this. age = "24";} var changeP = function (person) {person. age = "1"; person. name = "cheng"; person = null;} var p = new Person (); document. write ("Before change, p. name = "+ p. name + ", p. age = "+ p. age + "<br/>"); // before change, p. name = wang, p. age = 24 changeP (p); document. write ("after changing, p. name = "+ p. name + ", p. age = "+ p. age + "<br/>"); // after the change. name = cheng, p. age = 1 document. write (p = null); // false

In the above example, the parameter passed in changeP () is an object. When this method is called, it is not the object itself, but a copy of the object address value. Because the address value is the same as the address value of the original object, the attribute of the object can be modified. In p = null, the reference of the copy is disabled, it does not affect the original object. In java, the same result is obtained.

class Person{    String name;    String age;    public Person(String name,String age)    {        this.name=name;        this.age=age;    }    public String toString()    {        return "Person["+this.name+","+this.age+"]";    }}public class Test{    public static void changeP(Person p)    {        p.age="0";        p.name="cheng";        p=null;    }    public static void main(String[] args) {        Person p=new Person("wang","24");        System.out.println("before "+p);//before Person[wang,24]        changeP(p);        System.out.println("after "+p);//after Person[cheng,0]    }}

 

6. when a function with parameters in js is called, no parameters can be passed in. A method with parameters in java must be passed in. The function name is the unique identifier of the function in js, js does not have the concept of overloading in java.
 

    var method=function(t)    {        alert(t);    }    method();//undefined

If two functions with the same name are defined in the js Code, the subsequent functions will overwrite the previous functions even if the input parameters are different.

Var method1 = function () {alert ("constructor without Parameters");} var method1 = function (t) {alert ("constructor with Parameters ");} method1 (); // "constructor with Parameters"

As a weak language, the function that requires parameters in js usually needs to determine whether the passed parameters meet the requirements first.

Var changeName = function (p, newname) {if (typeof p = 'object' & typeof p. name = 'string' & typeof newname = 'string') {p. name = newname; document. write (p. name + "<br/>");} else {document. write ("the input parameter type does not meet the requirements" + typeof (p) + "" + typeof (newname) + "<br/>") ;}} changeName (); // The input parameter type does not meet the requirements of undefined changeName ("a", "s"); // The input parameter type does not meet the requirements of string person = {name: "wang"}; changeName (person, "zhang"); // zhang

 7. Object Attributes

You can use objName. propertyName to call the attributes of an object in js. The method of calling class attributes is class name + attribute name;

Unlike java, js attributes can be functions. In some cases, the objName [propertyName] method must be used to call js attributes.

Var Person = function (name, age) {this. name = name; this. age = age; this. show = function () {document. write ("this person's age:" + this. age + "name of this person:" + this. name + "<br/>") ;}} var p = new Person ("wang", "24"); for (var property in p) {// here, the object property must be called in the form of p [property], because if you use p. property method. js searches for the property in the p object to obtain the undefined document. write (property + "the property value is" + p [property] + "<br/> "); // running result // The value of the name attribute is wang // age. The value of the property is 24 // The value of the show attribute is function () {document. write ("this person's age:" + this. age + "name of this person:" + this. name + "");}}

8. attributes and methods of js objects and classes can be added at will.

The method for adding an attribute to a class is: Class Name. prototype. attribute name = something;

The method for adding a method to a class is: Class Name. prototype. method name = function (){};

Avoid defining methods directly in a class as much as possible, because this may cause memory leaks and closures. prototype makes the program safer and improves performance;

If a method is defined directly in the class, each time a new object of the class is created, a built-in method object is also created, which may cause memory leakage;

Add a method for the class using the prototype attribute so that all instance objects can share the method.

Var Person = function () {var s = "secret"; this.info = function () {return s ;}} var p = new Person (); var s = p.info (); alert (s); // secretPerson. prototype. test = function () {alert ("new method of p");} p. test ();

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.