变量java语言深入编写高级jscript代码(Java language depth written high-level JScript code)

jscript教程  时间:2021-01-29  阅读:()

java语言深入编写高级jscript代码Java language depth writtenhigh-level JScript code

1, create advanced objects

Use constructors to create objects

A constructor is a function call to instantiate and initializethe special type of object. You can call a constructor usingthe new keyword. A new example of using the constructor is givenbelow.

Var myObject = new (Object) ; / / create a generic object hasno attribute.

Var myBirthday = new Date (1961, 5, 10) ; / / create a Dateobject.

Var myCar = new (Car) ; // create an object of a user def ined,and initialize their properties.

A parameter is passed to the newly created null object by theconstructor as a value for a particular this keyword. Theconstructor then is responsible for performing adaptiveinitialization for the new object (creating attributes andgiving their initial values) . When finished, the constructorreturns a parameter of the object it constructs.

Write constructors

You can create objects and initialize them by using the new

operator in combination with predefined constructors likeObject () , Date () and Function () . The powerful feature ofobject oriented programming is the ability to define customconstructors to create custom objects that are used in scripts.Creates a custom constructor so that you can create objects thathave def ined properties. Here is an example of a custom function(note the use of the this keyword) .

When you call the Circle constructor, give the value of thecenter point and the radius of the circle (all of these elementsare necessary to completely define a unique round object) . Atthe end, the Circle object contains three attributes. Here ishow to instantiate a Circle object.

Var aCircle = new Circle (5, 11, 99) ;

Using prototypes to create objects

When you write a constructor, you can use the properties of aprototype object, which itself is an attribute of allconstructors, to create inheritance properties and sharemethods. The prototype properties and methods will be copiedto each object in the class by reference, so they all have thesame value. You can change the value of the prototype propertyin one object, and the new value overrides the default, but onlyin the instance. Other objects belonging to this class are notaffected by this change. An example of using a customconstructor is given below, Circle (note the use of the thisk eyword) .

Circle.prototype.pi = Math.PI;

Function, ACirclesArea () {

Return this.pi * this.r * this.r; / / circle area calculationformula for R2?.

}

Circle.prototype.area = ACirclesArea; / / the calculationfunction of the area of a circle is now a Circle Prototypeobject.

Var=ACircle.area (a) ; //this is howto call the area functionon the Circle object.

Using this principle, you can define additional attributes forpredefined constructors (both with prototypical objects) . Forexample, if you want to be able to delete the front and backspaces of the string (similar to theTrim functionof VBScript) ,you can create your own method for the String prototype object./ / add a function called trim as

A method for the prototype object / / the String constructor.String.prototype.trim = function ()

{

/ / with the regular expression before and after space

/ / with the empty string substitution.

Return (this.replace / (^\s*) | (\s*$) /g, "") ;

}

A blank string / /

Var s = "leading, and, trailing, spaces"";

Leading and trailing spaces / / display "(35)"

Window.alert (s +) (+ + s. length +) ;

/ / delete spaces before and after

S = s.trim () ;

Leading and trailing spaces / / display "(27)"

Window.alert (s +) (+ + s. length +) ;

2. recursion

Recursion is an important programming technique. The method isused to let a function call itself from within. One example isthe factorial calculation. The factorial of 0 is specificallydefined as 1. A larger number of factorial is obtained bycalculating 1 * 2 * *, with an increase of 1 every time, untilthe number of factors to be calculated is calculated.

The following paragraph is a function of the factorialcalculated in text.

"If this number is less than zero, then refuse to receive. ".If it is not an integer, it is roundeddown to adjacent integers.If this number is 0,

The factorial is 1. If this number is greater than 0, it ismultiplied by the factorial of the next smaller number. "To compute any factorial of a number greater than 0, at leastone factorial of the other number should be calculated. Thefunction used to implement this function is the functionalready in it; the function must call itself to compute the nextsmaller number of factorial before executing the current number.This is a recursive example.

Recursion and iteration (loops) are closely related.

Algorithms that can be handled recursively can alsobe iterated,and vice versa. Certain algorithms can usually be implementedin several ways. You just have to choose the most natural method,or the one that you think will be easiest to use.

Obviously, there could be a problem. A recursive function canbe easily created, but the function can not get a definiteresult and cannot reach an end point. Such recursionwould causethe computer to execute an infinite loop. Here is an exampleof the omission of the first rule (the processing of negativenumbers) in the text description of the factorial, and attemptsto compute the factorial of any negative number. This will leadto failure, because in order to compute the factorial of -24,

the factorial of -25 must first be computed; however, thefactorial of -26 must be calculated; so continue. Obviously,this will never reach a stop point.

Therefore, the design of recursive functions should beespecially careful. If you suspect the possibility of infiniterecursion in it, you can let the function record the number oftimes it calls itself. If the function calls itself too manytimes, it automatically exits even if you have decided how manytimes it should be called.

The following are still factorial functions, this time writtenin JScript code.

/ / function factorial. If passed

/ / invalid value (e.g. less than zero) ,

//will return to-1, show that the error occurred. If the valueis valid,

/ / numerical conversion to integer and the most similar./ / return factorial.

Function factorial (aNumber) {

ANumber = Math.f loor (aNumber) ; / / if the number is not aninteger, then rounding down.

If (aNumber < 0) {/ / if the number is less than 0, refused to

accept.

Return -1;

}

If (aNumber = = 0) {/ / if 0, the factorial of 1.

Return 1;

}

Else return (aNumber*factorial (aNumber- 1)) ; //otherwise,until the completion of the recursive.

}

3. variable range

JScript has two variable ranges: global and local. If a variableis declared outside any function definition, the variable isa global variable, and the value of that variable can beaccessed and modified throughout the entire range. If avariable is declared within the function definition, thevariable is a local variable. This variable is created anddestroyed every time the function is executed; and it cannotbe accessed by anything outside the function.

Languages such as C++ also have a"block scale"". Here, any ofthe "{}"are defined anew range. JScript does not support blockrange.

The name of a local variable can be the same as the name of aglobal variable, but this is a completely different andindependent two variable. Therefore, changing the value of avariable does not affect the value of another variable. Withinthe function declared local variables, only the local variablemakes sense.

Var aCentaur = "a horse with rider"; / / global definition ofaCentaur.

/ / JScript code, for the sake of brevity is omitted.Function antiquities (aCentaur) /a local variable declared inthis function.

{

/ / JScript code, for the sake of brevity is omitted.Var aCentaur="A, Centaur, is, probably, a, mounted, Scythian,warrior;

/ / JScript code, for the sake of brevity is omitted.ACentaur = "misreported that; is; / / add to local variables./ / JScript code, for the sake of brevity is omitted.} / / end function.

Var nothinginparticular = antiquities () ;

ACentaur as seen from a = "distance by a naive innocent";

*

Within the function, the value of the variable is "A, Centaur,i s, probab ly, a, mounted, Scythian, warrior. ",

Misreported; that is ";" outside of the function, the value ofthe variable is the remainder of the sentence:

“马和骑手从远处的天真无辜的。 ”

* /

很重要的一点是注意变量是否是在其所属范围的开始处声明的。有时这会导致意想不到的情况。tweak() v ar = 100

功能tweak() {

V aR的开始= 0 //显式声明新事物变量。

/ /本语句将未定义的变量赋给一些新事物 因为已有名为的局部变量。

许多新事物=

创梦网络-四川大带宽、镇江电信服务器云服务器低至56元

达州创梦网络怎么样,达州创梦网络公司位于四川省达州市,属于四川本地企业,资质齐全,IDC/ISP均有,从创梦网络这边租的服务器均可以备案,属于一手资源,高防机柜、大带宽、高防IP业务,一手整C IP段,四川电信,一手四川托管服务商,成都优化线路,机柜租用、服务器云服务器租用,适合建站做游戏,不须要在套CDN,全国访问快,直连省骨干,大网封UDP,无视UDP攻击,机房集群高达1.2TB,单机可提供1...

GigsGigsCloud(年付26美元)国际线路美国VPS主机

已经有一段时间没有听到Gigsgigscloud服务商的信息,这不今天看到商家有新增一款国际版线路的美国VPS主机,年付也是比较便宜的只需要26美元。线路上是接入Cogentco、NTT、AN2YIX以及其他亚洲Peering。这款方案的VPS主机默认的配置是1Gbps带宽,比较神奇的需要等待手工人工开通激活,不是立即开通的。我们看看这款服务器在哪里选择看到套餐。内存CPUSSD流量价格购买地址1...

BuyVM($5/月),1Gbps不限流量流媒体VPS主机

BuyVM针对中国客户推出了China Special - STREAM RYZEN VPS主机,带Streaming Optimized IP,帮你解锁多平台流媒体,适用于对于海外流媒体有需求的客户,主机开设在拉斯维加斯机房,AMD Ryzen+NVMe磁盘,支持Linux或者Windows操作系统,IPv4+IPv6,1Gbps不限流量,最低月付5加元起,比美元更低一些,现在汇率1加元=0.7...

jscript教程为你推荐
传奇类手游哪个好腾讯热血传奇手机版哪个职业厉害炒股软件哪个好网上买卖股票软件哪个好用杰士邦和杜蕾斯哪个好杰士邦的超薄款跟杜蕾斯的超薄款,哪个舒服点?雅思和托福哪个好考现在考雅思还是托福好美国国际东西方大学美国新常春藤大学有哪些?电信10000宽带测速电信宽带速度东莞电信宽带套餐东莞光纤宽带资费dns服务器未响应电脑上不了网了,显示DNS服务器未响应,什么意思360云盘网页版最近360云盘网页版登陆后,找不到文件共享群了。哪位知道在哪里可以进去文件共享群?360云盘下载别人在百度知道给了你360云盘资源,怎么在360云盘使用????
adman kvmla 香港服务器99idc 香港主机 idc测评网 宕机监控 lighttpd 一点优惠网 绍兴高防 赞助 免费美国空间 能外链的相册 下载速度测试 韩国代理ip 实惠 卡巴斯基试用版下载 广州服务器托管 免费主页空间 博客域名 web是什么意思 更多