JavaScript高级程序设计总结

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Candy_yl/article/details/79615216

//将其他进制的数值换算成十进制:
parseInt(“AF”, 16); //175
//将十进制数值换算成其他进制的数值:
var num = 10;
num.toString(2);//”1010”

null == undefined;//true

Number(null);//0
Number(undefined);//NaN

Number(“”);//0
parseInt(“”);//NaN

parseInt(” 1234blue”);//1234
parseFloat(“0xA”);//0

parseInt();//有第二个参数
parseFloat();//没有第二个参数

//每个字符串也都有一个toString()方法,该方法返回字符串的一个副本

Object的每个实例都具有下列属性和方法。

constructor;
hasOwnProperty(propertyName);
isPrototypeOf(object);
propertyIsEnumerable(propertyName);
toLocaleString();
toString();
valueOf();

// 按位非操作的本质:操作数的负值减1
var num1 = 25;
var num2 = ~num1;
console.log(num2);//-26

isFinite();//Infinity or -Infinity

0/0;//NaN
1/0;//Infinity
-1/0;//-Infinity
Infinity/1;//Infinity
Infinity/-1;//-Infinity

1 % Infinity;//1 如果被除数是有限大的数值而除数是无穷大的数值,则结果是被除数;

// 大写字母的字符编码全部小于小写字母的字符编码。
var result = “Brick” < “alphabet”; //true
var result = “Brick”.toLowerCase() < “alphabet”.toLowerCase(); //false
var result = “23” < “3”; //true
// “2”的字符编码是50,而”3”的字符编码是51。

undefined == 0;//false
null == 0;//false

var num = (5, 1, 4, 8, 0);//num的值为0

for (; ; ) { // 无限循环
// doSomething();
}

// 用来枚举对象的for (var key in object) {
// statement
}/ label语句
// 使用label语句可以在代码中添加标签,以便将来使用。

start: for (var i = 0; i < count; i++) {
        console.log(i);
}

// 这个例子中定义的start标签可以在将来由break或continue语句引用。加标签的语句一般都要与for语句等循环语句配合使用。

var num = 0;
outermost:
for (var i = 0; i < 10; i++) {
        for (var j = 0; j < 10; j++) {
                if (i == 5 && j == 5) {
                        break outermost;
                }
                num++;
        }
}
console.log(num); //55

// with语句的作用是将代码的作用域设置到一个特定的对象中。
with (location) {
var qs = search.substring(1);
var hostName = hostname;
var url = href;
}

Array.prototype.slice.call(arguments);

// 逻辑运算符的优先级:
// ! > && > ||

关于Date时间

var now = new Date(729878400000);//一般接收毫秒数,或者以下格式
// 根据特定的日期和时间创建日期对象(都返回表示日期的毫秒数)
Date.parse();
Date.UTC();
Date.parse("May 25, 2004");
Date.parse("2/17/1993");
Date.UTC(year: int, month: int, date?: int, hours?: int, minutes?: int, seconds?: int, ms?: int);
Date.UTC(2005, 0, 5, 17, 55, 55);

Date.now();

// 日期格式化方法
// 日期格式化为字符串的方法:
toDateString();
toTimeString();
toLocaleDateString();
toLocaleTimeString();
toUTCString();
toGMTString();

// 日期/时间组件方法
getTime();
setTime();
getFullYear();
getUTCFullYear();
setFullYear();
setUTCFullYear();
getMonth();
getUTCMonth();
setMonth();
setUTCMonth();
getDate();//几号
getUTCDate();
setDate();
setUTCDate();
getDay();//返回星期几
getUTCDay();
getHours();
getUTCHours();
setHours();
setUTCHours();
getMinutes();
getUTCMinutes();
setMinutes();
setUTCMinutes();
getSeconds();
getUTCSeconds();
setSeconds();
setUTCSeconds();
getMilliseconds();//毫秒
getUTCMilliseconds();
setMilliseconds();
setUTCMilliseconds();
getTimezoneOffset();//返回本地时间与UTC时间相差的分钟数

function.caller;
function.length;//函数希望接收的命名参数的个数
function.prototype;
function.apply(thisArg: any, argArray: Array);//环境对象、参数数组
function.call(thisArg: any, args…: any);//环境对象、参数
function.bind(thisArg: any, args…: any);//这个方法会创建一个函数的实例,其 this 值会被绑定到传给 bind() 函数的值。
window.color = “red”;
var o = { color: “blue” };
function sayColor () {
alert(this.color);
}
var objectSayColor = sayColor.bind(o);//会创建一个函数的实例
objectSayColor();//blue

// 基本包装类型
String.substring(start: int, end: int);

//基本包装类型
Boolean;
Number;
String;
typeof (new Boolean(true));//”object”
typeof (new Number(10));//”object”
typeof (new String(“true”));//”object”

(new Boolean(false)) && true;//true 布尔表达式中的所有对象都会被转换为true

Number.toFixed(fractionDigits: int);//按照指定的小数位返回数值的字符串表示
var num = 10;
alert(num.toFixed(2));//”10.00”
var num = 10.005;
alert(num.toFixed(2));//”10.01”
// 能够自动舍入的特性,使得 toFixed() 方法很适合处理货币值。

Number.toExponential(fractionDigits: int);//该方法返回以指数表示法(也称 e 表示法)表示的数值的字符串形式。
var num = 10;
alert(num.toExponential(1));//”1.0e+1”

Number.toPrecision(precision: int);//按照指定位数返回最适合的值
var num = 99;
alert(num.toPrecision(1));//”1e+2”
alert(num.toPrecision(2));//”99”
alert(num.toPrecision(3));//”99.0”

String.charAt(index);//按照索引得到字符
String.charCodeAt(index);//按照索引得到字符所对应的编码
String.concat();//拼接字符串
String.slice();//子集
String.substr();//第二个参数表示返回个数
String.substring();
String.indexOf();// 方向从前向后
String.lastIndexOf();// 方向从后向前

// 查找字符串中指定字符的所在位置(全局)
var stringValue = “Lorem ipsum dolor sit amet, consectetur adipisicing elit”;
var positions = new Array();
var pos = stringValue.indexOf(“e”);

while (pos > -1) {
positions.push(pos);
pos = stringValue.indexOf(“e”, pos + 1);
}
alert(positions);//”3,24,32,35,52”

String.trim();
String.trimLeft();
String.trimRight();

String.toLowerCase();
String.toLocaleLowerCase();
String.toUpperCase();
String.toLocaleUpperCase();

String.match();//在字符串上调用这个方法,本质上与调用RegExp的exec()方法相同。
String.search();
String.replace();
var text = “cat, bat, sat, fat”;
var pattern = /.at/;
// pattern.exec(text);
text.match(pattern);//[“cat”]
text.search(/at/);//1
text.replace(/at/g, “ond”);//”cond, bond, sond, fond”

// 根据匹配项的不同返回不同的结果
function htmlEscape (text) {
return text.replace(/[<>”&]/g, function (match, pos, originalText) {
switch(match) {
case “<”:
return “<”;
case “>”:
return “>”;
case “&”:
return “&”;
case “\”“:
return “"”;
}
});
}
alert(htmlEscape(“

Hello world!

“));
//<p class="greeting">Hello world!</p>

String.split();
Array.join();

String.localeCompare();//比较两个字符串在字母表中的位置

String.fromCharCode(104, 101, 108, 108, 111);//”hello”
(“h”).charCodeAt();//104

// 单体内置对象
// 在所有代码执行之前,作用域中就已经存在两个内置对象: Global 和 Math 。
encodeURI();// 不会对本身属于 URI 的特殊字符进行编码,例如冒号、正斜杠、问号和井字号
encodeURIComponent();// 会对它发现的任何非标准字符进行编码
decodeURI();// –> encodeURI() 反解析
decodeURIComponent();// –> encodeURIComponent() 反解析

eval();
eval(“alert(‘hi’)”);

// 通过window对象取得Global对象
// 另一种取得 Global 对象的方法是使用以下代码:

var global = function () {
        return this;
}();

Math.max(3, 54, 32, 16);
Math.min(3, 54, 32, 16);

var values = [1, 2, 3, 4, 5, 6, 7, 8];
var max = Math.max.apply(Math, values);

Math.ceil();
Math.floor();
Math.round();
Math.random();
// 一定范围的随机值 = Math.floor(Math.random() * 可能值的总数 + 第一个可能的值)
Math.abs();
Math.exp();
Math.log();
Math.pow(num, power);//返回 num 的 power 次幂
Math.sqrt();//返回 num 的平方根
Math.acos();
Math.asin();
Math.atan();
Math.atan2(y,x)
Math.cos();
Math.sin();
Math.tan();

猜你喜欢

转载自blog.csdn.net/Candy_yl/article/details/79615216