JS or C #? Contention for non-existent scripts

Source: Internet
Author: User

Objective:

Again came to the weekend, little Bastard also finally have a breath to write blog time and energy. In the afternoon of Friday, Little bastard accidentally shook a bull's QQ group, and saw a unity3d development Laosheng long talk, "My development language is to choose JavaScript?" Or is it C #? ”。 To this question, small bastard also think really should carefully comb a bit. So why does the debate between JavaScript and C # not exist at all? First, we need to know what the JS script in Unity3d is. The most accurate scientific name, I think, should be called Unityscript (because the code unity-technologies on GitHub is called ...). )。 So why is Unityscript not javascript? Let's continue to analyze Unityscript vs JavaScript. Since little Bastard says Unityscript is not JavaScript, Unity3d does have several scripting languages (C#,unityscript, Boo), and how do we choose to compare it to C #? So the last little bastard will analyze C # vs Unityscript.

The most familiar stranger to--unityscript:

Little Bastard's previous article, "Why does mono cross-platform?" Talk about CIL (MSIL) describes the basics of Unity3d cross-platform. That is, by using CIL, the source code (for example, C #) is compiled into CIL (in fact, CIL assembly), and then the CIL is compiled into the native code of the target platform by JIT or FULL-AOT, thus implementing a set of code, cross-platform functionality used by multiple platforms. So you can imagine, as Unity3d in the "JS" (I will use Unityscript to call) will also be compiled into CIL code, and then compiled into the corresponding platform of the native code. In other words, Unity3d implements a set of language unityscript that are "at the same level" on the. NET Platform and C #. Developed a set of their own language, which sounds crazy, right? It's not really a bit. Just said, the source code must be compiled into CIL, so as long as a set of compilers can be UNITYSCRIPT syntax analysis to identify and compile to CIL, the problem is not solved? Of course, if there are ready-made, can meet the following three of the language as a reference, the UNITYSCIRPT developers should be grateful for it. What three points?

    1. is the language of the. NET level.
    2. Like a scripting language
    3. A compiler can compile it into CIL

That's right, smart crossing. You must have thought, that has been hidden deep behind the characters, Unity3d script one of the Big Three boo. All say Boo is one of the scripting languages of Unity3d, but why haven't you seen anyone use it? One reason may be that Unity3d first introduced Boo, purely for unityscript service. Boo is a third-party language for the. NET platform and is written much like a scripting language, and has a corresponding compiler that can implement the process from boo to CIL. Boo writes like this: (more about Boo see the new language--boo above the CLI)

Import Systemimport system.netimport system.threadingurl, local = Argvclient = WebClient () call = client. Downloadfile.begininvoke (URL, local) while isn't call. IsCompleted:        Console.Write (".")        Thread.Sleep (50MS) Console.WriteLine ()

In short, Unityscript is born out of Boo. Although it looks like Unityscirpt and Boo don't look alike, it is not a problem to understand that grammar is the semantics. As long as the unityscript syntax can be parsed into Boo's compiler can understand, then the Unityscript compiler of most of the work can be handed to Boo's compiler to implement. So, based on the out-of-the-box boo language, the work of developing the Unityscript compiler is only parsing, and indeed they do.

And when we came to Unityscript on GitHub's hosting page and found that Boo's developer was maintaining, and Unityscript's compilation logic was in the file that was a boo file--unityscriptcompiler.boo.

Although you don't know the boo language, you can still see the compiler that will introduce boo:

Import Boo.Lang.Compiler

There will also be a process for parsing the syntax of Unityscript for Boo's compiler to recognize:

Pipeline. Insert (0,unityscript.steps.preprocess ()) pipeline. Replace (Boo.lang.compiler.steps.parsing,unityscript.steps.parse ()) pipeline. Replace (Boo.lang.compiler.steps.introduceglobalnamespaces,unityscript.steps.introduceunityglobalnamespaces ()) Pipeline. InsertAfter (Boo.lang.compiler.steps.preerrorchecking,unityscript.steps.applysemantics ()) pipeline. InsertAfter (Unityscript.steps.applysemantics,unityscript.steps.applydefaultvisibility ()) pipeline. InsertBefore (Boo.Lang.Compiler.Steps.ExpandDuckTypedExpressions, UnityScript.Steps.ProcessAssignmentToDuckMembers ()) pipeline. Replace (Boo.lang.compiler.steps.processmethodbodieswithducktyping,unityscript.steps.processunityscriptmethods ( ))

Well, I'm not going to tell you. On the same GitHub page the introduction of Unityscript is:

A JavaScript Implementation based on the Boo programming language.

So where exactly does the Unityscript compiler put it? (from Unityscript to CIL) See:

Path to the Mac version:/applications/unity/unity.app/contents/frameworks/mono/lib/mono/unity/us.exe

Path to win version: U3d path/editor/data/mono/lib/mono/unity/us.exe

Say so much, still need for our this article of the protagonist Unityscript name: is static language and need to compile, born out of Boo language, and JavaScript in addition to the suffix does not have a relationship. So instead of "how u3d JavaScript really resembles JavaScript", consider how unityscript and Boo are.

Unityscript vs JavaScript

Many of the bastard mentioned in the preamble of the old problem of the basin oil, often think unityscript and we usually say that JavaScript is the same, even if some people realize that there is a difference, but it is still in nature and JavaScript belong to the same category. Period Lower primary Bastard also think so, because once used cocos2d-js development game, so when heard Unity3d he meow unexpectedly support JS script, that call a happy AH. But the initial happiness often hints at the last disappointment.
Because the last little bastard realized that Unityscript and JavaScript are two languages that are quite different.
In fact, the so-called JavaScript is a very general name, can be used to refer to any implementation of the ECMAScript standard language (for JS big God light spray ... ), and Unityscript subjectively did not attempt to achieve even close to the standard. So a lot of JavaScript library if only a simple copy paste, in the Unity3d will not run particularly "smooth" (the reason is quoted because a lot of things can not run up it).
So how much difference does the two have? The main and essential difference before the small bastard has already said, it is similar to the two, in addition to the appearance, there is no contact. But there is still a need for a more intuitive understanding. So small bastard Simply summarize the difference between JavaScript and Unityscrip.

JavaScript does not have the concept of class

JavaScript does not have a class concept. Because it is a prototype-based language, inheritance occurs between objects and objects (more flexible, in other words, more dynamic) than between classes and classes.

As an example:

function person () {   this.name = ["Chenjiadong"],} var c = new person (); Console.log (typeof C.introduce); Person.prototype.introduce = function () {   console.log ("I am  

As you can see, in the Bastard limited JS knowledge, there is also the concept of objects and prototypes. With the keyword new, we can create an object C with the method person, and the small c is still very simple and will not introduce itself. But if the person has evolved and learned to introduce himself introduce, the previously created little C also learned to introduce himself introduce.

But this is not possible in unityscript, because it has the concept of a class. Once you have defined your class, the class cannot be changed while the program is running.

The fastidious of file name

I don't know if you have any wood in the oil basin, a new Unityscript file does not seem to declare a class. Instead, it looks much like the general JavaScript notation. such as this test.js:

#pragma strictfunction Start () {}function Update () {}

In fact, the reason is the file name. Most of the. js files in u3d represent a class, so naturally, the name of the file is used to address the class. (Of course, most files mean that you can also define multiple classes in a file, such as classes that do not inherit from Monobehav).

So the code above is equivalent to this C # code:

Using Unityengine;public class Test:monobehaviour {    void Start () {    }        //Update is called once per frame
   
    void Update () {            }}
   

The inverse of JavaScript is simply executing the code in the file, regardless of the filename.

In addition to these two, compared to the "macro" difference, is there any difference in grammar ? Have AH ~ Crossing, you continue to look good son ~

Syntax differences can declare several variables at a time

In JavaScript, you can often do this:

var x = 1, y = 2;

But in Unityscript, u3d usually say so.

The necessity of the semicolon of grammatical differences

In the past, I remember seeing a university professor spit out a JavaScript "bug" on Weibo, (Little bastard actually found that last year's Weibo with his memory ...). Praise)

So, one of the features of JavaScript that lets Beiligong teachers fall into the hole is that the program will default at the end of the line when it executes ";" In other words, as humans or apes we can not write semicolons at the end of a row.

and unityscript to avoid this is likely to create a bug, and the semicolon made the request. I won't have to say more about it next.

Anyway, you don't write a semicolon, u3d would probably say:

Can the "assignment expression" of a grammatical difference be assigned as an expression?

To tell the truth, Little Bastard also do not know how to think of such a small title of a detour. But little Bastard remembers in JavaScript that it can be written like this:

var x = 3; X is 3var y = (x=x+2); X is 5 and Y is 5

So here, x=x+2, this assignment expression is assigned y as an expression.

But the same is not supported in Unityscript. This u3d will be a lot of talk with you.

The Var and the type of grammatical differences that's something.

The topic that JavaScript is not going to go around is the Var that declares the variable. Assuming that you declare a variable without VAR, the variable defaults to the global variable. However, declaring variables without a VAR is not feasible in unityscript.

Well, that compromise, in the unityscript I take Var still not? Does that make me fly like a JavaScript, on a type of freedom?

For example, the following code, I am working on JavaScript and unityscript is perfect ah.

Unityscript and JavaScript can both run hey ~var x;x = 3;x = new Array ();

It looks like Unityscript is a dynamic language! It's kind of like nothing. A lot of oil here is confused, and then produced the illusion.

In fact, as a static language, when the compiler compiles, the variable is what type it can be cleared, so in the Unityscript, the above code in the X exactly what type but OK. That's weird, isn't it? Well, let's write the equivalent code, and you crossing understand.

In Unityscript is equivalent to the following object x;x = 3;x = new Array ();

Yes, the original X is the object type. All types are derived from it, in other words, all types can be converted to it. So 3 can be converted into Object,array can also be converted to object. This is why Unityscript appears to be a dynamic type. (Frequent boxing, will certainly affect efficiency)
So if you call an object that doesn't have a method, there might be a hint:

Of course, there are a lot of topics about Var, like C #, but it's not exactly the same. Also, there are many topics that JavaScript and unityscript can talk about, but they are limited in length to stop here.

C # vs Unityscript

Finally came to our frequently debated Gunpowder Bucket topic, in the End C # and Unity3d js who good?

The most common problem is: with JS written u3d game and C # written u3d game, in the end who is running high efficiency ah?

The most common answer is no: C # Certainly, because JS is dynamic. Certainly not as good as the compiled language.

The second common problem is: using JS Development and C # development, which is faster and better for me ah?

The second most common answer is:JS suitable for personal development, agile fast. C # Suitable for company development, strict specifications.

We still use the same ideas as the one we discussed with JavaScript to sort out the differences between C # and Unityscript, that is, the order of the first essence and then the performance. At the same time answer the above two questions.

Essential differences

At the beginning, Unityscript is the same level of language as C #, and it also needs to go through the transition from source code to CIL intermediate language, eventually to the process of compiling to native language. So essentially, the final run is the native machine language compiled from CIL. But there is a rapid phenomenon of C #, so what is the problem?

One possible but not the only answer is that Unityscript and C # generate CIL intermediate languages differently.

This is also very simple to think about, as mentioned in the question of Var, if the use of object to deal with the problem of Var, it is inevitable that the frequent packing and unpacking operations, which has a great impact on efficiency.

So it is true that C # is faster, but the reason is that unityscript involves frequent packing and unpacking operations, and the resulting CIL code differs from C # rather than unityscript as a dynamic language and not compiled.

The reality is simple.

is development using C # or Unityscript? If you do not consider the efficiency of the operation, just consider the experience of development time, small bastard talk about their own views good-that is to cherish time, away from Unityscript.

First, there are a few facts we need to know:

    1. Unityscript is a third-party language derived from the. NET platform Boo. The so-called third-party language and C # differences, with their own in the end is not the real, dad is not the next door to the old king is the same. The gap may be all-encompassing, three-dimensional. Community support, code maintenance, and even the quality of the compiled CIL code can be a big gap. Before you choose Unityscript, ask yourself have you heard boo before? Don't forget the origins of Unityscript and Boo.
    2. Unityscript and JavaScript have nothing to do but look alike. You are a duck in JavaScript, and if you are careless in unityscript you may bury the hidden danger, and some hidden dangers may be hidden deep. And Unityscript is also static language, also need to compile, so do not see how to choose it as a development language why people feel fast.
    3. Plug-in support. It seems that most of them are written in C #.
    4. Well, if you can't be persuaded by the 3 points above, look at the official attitude.

U3d The official team based on data analysis a percentage chart of the language used.

Since the use of the boo language is largely negligible, the document support for Boo is stopped starting with the Unity5.0 version. Also disappears is the option to create Boo script from the menu, "Creation Boo script." From the U3D team's attitude to boo, you can also get a glimpse of the future trend of unityscript in close contact with Boo.

At the same time, the U3D team will shift the focus of support to C #, which means that documents and samples, as well as the community-supported focus on c#,c# documents, will be the most complete, and C # code instances will be the most detailed, and the number of people in the community that will be discussed in C # is the most.

So in the end how to choose, you can also have their friends have their own understanding.

It is late, bastard go to sleep also ~

Ps:

Got up from the bed, forgot to write "kneeling for recommendation" =. =

Pretending to be StatementOnce: This Bo article is not special note is original, if need to reprint please keep the original link (http://www.cnblogs.com/murongxiaopifu/p/4228754.html) and author information Murong Small Bastard

JS or C #? Contention for non-existent scripts

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.