变量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 //显式声明新事物变量。

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

许多新事物=

hostkey俄罗斯、荷兰GPU显卡服务器/免费Windows Server

Hostkey.com成立于2007年的荷兰公司,主要运营服务器出租与托管,其次是VPS、域名、域名证书,各种软件授权等。hostkey当前运作荷兰阿姆斯特丹、俄罗斯莫斯科、美国纽约等数据中心。支持Paypal,信用卡,Webmoney,以及支付宝等付款方式。禁止VPN,代理,Tor,网络诈骗,儿童色情,Spam,网络扫描,俄罗斯色情,俄罗斯电影,俄罗斯MP3,俄罗斯Trackers,以及俄罗斯法...

数脉科技8月促销,新客减400港币,BGP、CN2+BGP、阿里云线路低至350元

数脉科技(shuhost)8月促销:香港独立服务器,自营BGP、CN2+BGP、阿里云线路,新客立减400港币/月,老用户按照优惠码减免!香港服务器带宽可选10Mbps、30Mbps、50Mbps、100Mbps带宽,支持中文本Windows、Linux等系统。官方网站:https://www.shuhost.com* 更大带宽可在选购时选择同样享受优惠。* 目前仅提供HKBGP、阿里云产品,香港...

搬瓦工:香港PCCW机房即将关闭;可免费升级至香港CN2 GIA;2核2G/1Gbps大带宽高端线路,89美元/年

搬瓦工怎么样?这几天收到搬瓦工发来的邮件,告知香港pccw机房(HKHK_1)即将关闭,这也不算是什么出乎意料的事情,反而他不关闭我倒觉得奇怪。因为目前搬瓦工香港cn2 GIA 机房和香港pccw机房价格、配置都一样,可以互相迁移,但是不管是速度还是延迟还是丢包率,搬瓦工香港PCCW机房都比不上香港cn2 gia 机房,所以不知道香港 PCCW 机房存在还有什么意义?关闭也是理所当然的事情。点击进...

jscript教程为你推荐
郑州软银科技有限公司河南/郑州网站设计公司哪家做的最好呀?てっっっ月付百万的女人们我们家的女人们92集在线观看 韩剧我们家的女人们92中字 我们家的女人们93集快播下载腾讯空间首页qq空间主页怎么每个都看不见租车平台哪个好租车哪个平台好点,都要什么费用?p图软件哪个好用什么p图软件好用?不是p人照片的那种软件聚酯纤维和棉哪个好聚酯纤维和棉哪个好集成显卡和独立显卡哪个好集成显卡与独立显卡的区别。江门旅游景点哪个好玩的地方江门有什么地方好玩的?唱K 行街 免答小说软件哪个好用免费现在看小说用什么软件好,不用钱的,绝地求生加速器哪个好现在绝地求生哪个加速器好点?
个人注册域名 locvps bandwagonhost 一点优惠网 私有云存储 网通服务器ip 云鼎网络 太原联通测速平台 创梦 天互数据 个人域名 什么是刀片服务器 国外免费全能空间 cdn加速是什么 中国电信宽带测速网 ftp免费空间 彩虹云 web服务器是什么 英国伦敦 东莞主机托管 更多