首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

javascript当中null和undefined的==和===的比较

例 3.1.3(null和undefined的==和===的比较) <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> </head> <script> //马克-to-win:声明变量x /* if a value is not set, its typeof is undefined, its value is undefined, if a value= null, its typeof is object, its value is null,but when you use == to test, they are the same, but when to use === to test, they are not the same,== means as long as value is the same, ok, but === means type also must be equal. */ var x; var z1 = "d"; var y = null; // document.writeln("z1 is"+z1); /*if you cancel commenting out the following statement, it will give the blank page, which means it has error.*/ // document.writeln(zyx); document.writeln(x + " is x"); document.writeln(typeof(x) + " is typeof(x)"); document.writeln(y + " is y"); document.writeln(typeof(y) + " is typeof(y)"); var z = undefined; document.writeln(z + " is z"); document.writeln(typeof(z) + " is typeof(z)"); if (y == undefined) { document.writeln('null and undefined is interchangable'); } if (z1 != null) { document.writeln('z1 != null'); } if (y === undefined) { document.writeln('null and undefined is exactly the same'); } if (x == undefined) { document.writeln('声明变量后默认值为undefined'); } if (x === undefined) { document.writeln('声明变量后默认值为exactly the same as undefined'); } if (x == null) { document.writeln('声明变量后默认值为null'); }

00
  • 您找到你想要的搜索结果了吗?
    是的
    没有找到

    javascript当中静态方法和prototype用法

    6)静态方法和prototype(难) 例 3.6.1 <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> </head> <script> /*note that 马克-to-win: static variable's value has nothing to do with instance's variable's value.instance 名称 can not 直接access static member like in java. This is different from Java,比如下面例子中,Student.number=2,但是d1.number就为undefined.This is different from Java,但在实例方法中(比如d1.info)可以访问Student.number。这是和java中一样的。或者说function外或任何地方都可以访问Student.number。反过来,d1.age也可以在静态方法中访问,就像在function外一样,任何地方都能访问d1.age。String.prototype.abcd,这是给所有的实例加属性而不是静态属性。*/ function Student(number, agev) { this.age = agev; /*static variable's value can not be accessed by instance */ Student.number = number; /*lb is local variable, but not a member variable because it is not modified by this. from outside it can not be accessed. refer to noblockScope.html */ var lb = 0; } var d1 = new Student(1, 3); document.writeln("this的age属性为means window.age" + this.age + "
    "); document.writeln("d1的age属性为" + d1.age + "
    "); document.writeln("d1的number属性为" + d1.number + "
    "); document.writeln("通过Student访问静态number属性为" + Student.number + "
    "); document.writeln("d1的lb属性为" + d1.lb + "


    "); d1.qixy = "abc";/*以随意为实例加属性或方法*/ document.writeln("可以随意为实例加属性或方法see following,d1的qixy属性为" + d1.qixy + "

    "); document.writeln("是否有静态变量qixy" + Student.qixy + "

    "); d1.info = function()/*此方法仅为d1对象所用*/ { document.writeln("对象的qixy属性:" + this.qixy); document.writeln("对象的age属性:" + this.age); /*下列话是合法的, 因为不是this.number, 而是Student.number*/ document.writeln("static method is " + Student.number); }; Student.prototype.infop = function()/*此方法可以为所有Student对象所用*/ { document.writeln("对象的qixy属性p:" + this.qixy); document.writeln("对象的age属性p:" + this.age);

    02

    javascript当中arguments用法

    8)arguments 例 3.8.1 <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> </head> <script> /* when there are n functions with the same function name, only the last one is used. */ function test() { document.writeln("no argument constructor."); } function test(person) { document.writeln("马克-to-win2"); /*Function.arguments[] (Collection) The values passed to the function when it is called. 马克-to-win: inside function,we can directly use arguments. */ var n = arguments.length; document.writeln("n is " + n); for (var i = 0; i < n; i++) { document.writeln("马克-to-win3"); document.writeln(arguments[i]) } document.writeln(person); document.writeln(typeof(person) + "is typeof"); } /*when no param, undefined is passed in. no overloaded function concept.*/ test(); /*when there is no this function, program stops here.*/ change(); document.writeln("finally"); </script>

    01

    javascript当中局部变量和全局变量

    2)局部变量和全局变量 浏览器里面 window 就是 global,通常可以省。 nodejs 里没有 window,但是有个叫 global 的。 例 3.2.1 <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> </head> <script> /* 有var无var, 在function外是一样的,都是全局的,在function里面时,var是局部的,而无var时是代表全局的*/ var testVar = "全量"; document.writeln("window.testVar is" + window.testVar+testVar); var testqVar = "全量q"; /*如不屏蔽下句话,程序直接停在这了,因为出错了,不认识testGlobal,得把下一句和下下句换一下位置,就ok了 */ // document.writeln("testGlobal is" + testGlobal); testGlobal = "全量global"; document.writeln("abc is" + abc); var abc; testGlobalInVar = "全量globalInVar"; function testSco() { var lll = "qqq"; var testVar = "局量"; //此testVar非外面的testVar testqVar = "全量qchange"; //此testqVar就是外面的testqVar testGlobal = "全量globalchange"; var testGlobalInVar = "局量global";//此testGlobalInVar非外面的testGlobalInVar /*local variable is stronger than global variable.so "testVar" in the following statement means local variable.*/ document.writeln(testVar); document.writeln(testqVar); document.writeln("testGlobalInVar is " + testGlobalInVar); } testSco(); document.writeln("second test is " + testVar); document.writeln("second testqVar is " + testqVar); document.writeln("testGlobal is " + testGlobal); document.writeln("testGlobalInVar is " + testGlobalInVar); </script>

    00

    javascript当中的构造函数的用法

    5)构造函数的用法: 例 3.5.1 <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> </head> <script> function Student(name, age) { /* 马克-to-win:later on we can use it in var doc = new ActiveXObject( "Microsoft.XMLDOM" ); doc.async="false"; doc.load(str); when a property has a this, means that this property is a member property. */ this.name = name; this.age = age; this.parti = function() { document.writeln("名字是:" + this.name + "
    "); document.writeln("年纪是:" + this.age + "
    "); }; } var p = new Student('jeri', 3); document.writeln("typeof p is " + typeof(p)); //typeof(p) is object p.parti(); p.age = 4; p.parti(); /*the following two methods can also access some properties.*/ document.writeln("" + p["age"]); document.writeln("" + p["a" + "ge"]); if (p instanceof Student) document.writeln("p是Student的实例
    "); /*javascript 中的对象全部是Object 的子类 Because this object is the topmost parent object in the prototype inheritance hierarchy, all other object classes inherit its methods and properties. It's a close enough call that JavaScript 2.0 may well move it into the class-based object-oriented category at which time the prototype inheritance would be replaced with super-class/sub-class mechanisms and the arguments become null and void. */ /*When the Global object is created, it always has at least the following properties: Object object Function object Array object String object Boolean object Number object Date object Math object Value properties */ if (p instanceof Object) document.writeln("p是Object的实例"); </script>

    00

    javascript当中prototype用法

    prototype 见上一节,马克-to-win:prototype作用就是给某个类增加一个实例方法。 例 3.6.2 <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> </head> <script> /*When the Global object is created, it always has at least the following properties: Object object Function object Array object String object Boolean object Number object Date object Math object Value properties */ Array.prototype.mymethod = function(number) { var result = -1; /*注意mymethod功能是找出某数在数组中出现的位置。作为Array的一个function,可以访问Array的属性this.length, 参见上一个prototype的例子, Student.prototype.infop = function()/*此方法可以为所有Student对象所用*/ { document.writeln("对象的qixy属性p:" + this.qixy); document.writeln("对象的age属性p:" + this.age); /*下列话是合法的, 因为不是this.number, 而是Student.number*/ document.writeln("static method is " + Student.number); }; */ for (var i = 0; i < this.length; i ++) { if (this[i] == number) { result = i; break; } } return result; }

    01
    领券