JS and Java all kinds of pit daddy

Source: Internet
Author: User

1.js string Pit Daddy's "= ="

JS string type is different from Java string type, when compared with equals, you can use "= =" directly.

Test, this "= =" seems to compare the pit father

<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 there be such an unreasonable thing? A=c,b=c, but a is not equal to b!!!.

My conclusion is that the string type in JS does not have the Equals method, but when the string type object is compared to another string type variable, the method used is similar to the Java equals method. When compared with two string types, "= =" is similar to "= =" in Java, which compares two objects as if they were the same object.

The corresponding Java code, there is no such a tangled place

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   

The string object created by new in Java is compared with "= =" when compared to the address value of the object, whereas in JS, the address value is compared only if two objects are created with new.

The similarities and differences between the array in 2.js and the list in Java:

Same point:

1. The added element type can be different. (after Java uses generics, only the specified element type can be added)

2. Variable length

Different points:

JS can access more than the array length of the corner label, return Undefined,java, List.get (index) index value exceeds the length of the array, the runtime has an exception: 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. function in JS

JS function can be considered as a class, its properties are divided into://Instance properties: Use this as a prefix//class Property: Use the function name as prefix//local variable: using var declaration or direct assignment/* Unlike Java, JS class properties can only be accessed through classes, not enough objects to access * In the same class, you can have class properties and instance properties of 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", "+"), 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}        

The properties of the JS object can be added arbitrarily
The above P object does not have the T, me property, after adding:

P.me= "OK";p. t= "Ttok";d ocument.write (p.t+ "<br/>");//ttokdocument.write (p.me+ "<br/>");//ok 

The function in 4.js is independent, it does not belong to any class or object;

Although a function in JS can be defined in a class, it can be called by objects of other classes, whereas methods in Java are instance methods or static methods (class methods) that belong to objects or classes. The call () method acts: Enables different objects to invoke a method.

Syntax format for call: function name. Invoke (Calling object, Parameter 1, parameter 2) Without parameters, you can omit Var wolf=function(name,voice,action) {     this.name=name;     This.voice=Voice;     this.action=Action;     This.info=function() {         document.write (this.name+) "+this.voice+" is the way to eat food: "+this.action+" <br/> " ); }} new Wolf ("Wolf", "ao~", "bite"). info ();//wolf's cry is the way ao~ eats food: 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 ~ "," sold Moe ");//Cat's Cry is miao~ eat food way is: Sell moe         

The parameter passing of the 5.js function is the same as the parameter passing of the Java method: All values are passed

    var person=function()    {        this.name= "Wang";        This.age= "";    }    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/>");//change before p.name=wang,p.age=24 CHANGEP (p); document.write ("Changed, P.name=" +p.name+ ", p.age=" + P.age+ "<br/>");//Change, p.name=cheng,p.age=1 document.write (p==null);//false     

In the above example, the parameter passed in CHANGEP () is an object, and when called, the actual pass-through is not the object itself, but rather a copy of the object's address value. Because this address value is the same as the address value of the original object, it is possible to modify the object's properties, while in P=null, where the reference to the copy is cut off, it has no effect on the original object. In Java, the results of the calculations are the same.

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;} PU Blic static void main (string[] args) {person p=new person ("Wang", "" "); System.out.println ("before" +p);//before person[wang,24] CHANGEP (p); System.out.println ("after" +p);//after person[cheng,0] }}        

6.js parameters of the function in the call, you can not pass in parameters, Java methods must pass parameters, the function name is JS in the function of the unique identifier, JS does not have the concept of overloading in Java.
 

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

If you have defined two functions with the same name in the JS code, the function will overwrite the previous one, even if the arguments passed in by the function are different.

var method1=function()    {        alert ("constructor without arguments");    }    var method1=function(t)    {        alert ("constructor with arguments");    }    Method1 ();//"constructor with parameters"   

As a weakly typed language, the function that needs parameters in JS is usually judged whether the incoming parameters meet the requirements.

    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 ("Incoming parameter type does not meet requirements" +typeof (p) + "" +typeof (newname) + "<br/>");}} changename () The incoming parameter type does not meet the requirements undefined undefined changename ("a", "s");//The parameter type passed in does not meet the requirements string string person={name: "Wang"}; ChangeName (person, "Zhang");//Zhang      

7. Properties of an object

JS in the invocation of an object's properties can be objname.propertyname, the way to invoke the Class property is the class name + attribute name;

Unlike Java, JS's properties can be functions, and in some cases, invoking the JS attribute must use Objname[propertyname]

var person=function(name,age) {    this.name=name;    This.age= age;    This.show=function() {        document.write ("Person Age:" +this.age+ "This person's name:" +this.name+ "<br/>");}    } The Var p=new person ("Wang", "" "), andfor (Var properties in P) {//Here must use P[property] to invoke the object property, Because if you use P.property, JS will look in the P object named property, get undefined document.write (property+ "property value is" +p[property]+ "<br/ > "); The value of the run result//Name property is the value of the wang//age property is the value of the 24//Show property is function () {document.write ("This person Ages:" +this.age+ "This person's name:" +this.name+ "");}} 

8.js objects, class properties, and methods can be added arbitrarily

The way to add properties to a class is the class name. prototype. attribute name =sth;

The way to add a method to a class is: Class name. prototype. Method name =function () {};

You should try to avoid defining the method directly in a class, as this can lead to memory leaks and closures, prototype the way the program is more secure, and improves performance;

If a method is defined directly in a class, then each new object of that class is created, and an object of the built-in method is made, which is prone to memory leaks;

Add a method to a class by using the prototype property 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 for "P") );} P.test ();       

JS and Java all kinds of pit daddy

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.