随笔分类 - JavaScript
JavaScript学习笔记
摘要:1. 把对象作为参数传入functionvar a = [1,2,3,4];var b = [];function arraycopy(/* array */ from, /* index */ from_start,/* array */ to, /* index */ to_start,/* integer */ length) { var j = to_start - 1; for(var i = from_start - 1; i < length; i++) { to[j] = from[i]; j += 1; }}function copyArray(arrObj...
阅读全文
摘要:一般的面向对象程序语言,有两种继承方法——接口继承(interface inheritance)和实现继承(implementation inheritance)。接口继承只继承方法签名,而实现继承则继承实际的方法。在JavaScript中,函数没有签名,所以在JavaScript只支持实现继承,而且主要是依靠原型链(prototype chaining)来是实现的。原型链(prototype chaining):利用原型来继承属性和方法。回顾一下构造函数(constructor),原型对象(prototype)和实例(instance)的关系。每一个构造函数都有一个prototype属性,该
阅读全文
摘要:for循环:for(i=start; i<end; i++) {}while循环: (注意, 若条件一直为真, 则会进入死循环, 浏览器就hang掉)while (condition) { //do something; //change condition;}递归:使用for循环做substringfunction substring(all, start, end) { for(i=start; i<=end; i++) { console.log(all[i]); }substring("eclipse", 1, 4); //clip使用递...
阅读全文
摘要:JavaScript里的Class(类),其实是通过Function来实现的。我们通过字面量或者构造函数来创建对象时,其实都只是给特定的一个对象赋属性和值。如果我们有多个对象,他们的属性都一样只是值不同,那就会写很多重复的语句,这时候类就很好用了。以下为构造函数方法创建类:function className (prop_1, prop_2, prop_3) { this.prop1 = prop_1; this.prop2 = prop_2; this.prop3 = prop_3;}有了上面的类,我们就可以为类创建实例:var obj_1 = new className(v1, v2...
阅读全文
摘要:1. 2 ways to create Object in JavaScriptLiteral notation is the one where we declare the object, and, at the same time, define properties and values in the object. It makes use of { }. See first one for an example.//Literalvar australia = { weather: "superb", people: "not many of them
阅读全文
摘要:Math.random():获取0~1随机数Math.floor() method rounds a number DOWNWARDS to the nearest integer, and returns the result. (小于等于 x,且与 x 最接近的整数。)其实返回值就是该数的整数位:Math.floor(0.666) --> 0Math.floor(39.2783) --> 39所以我们可以使用Math.floor(Math.random())去获取你想要的一个范围内的整数。如:现在要从1~52内取一个随机数:首先Math.random()*52 //这样我们就能
阅读全文

浙公网安备 33010602011771号