js-ecma2

#ECMA第二篇

##

对象是属性的集合

读取当前对象的属性

(使用操作符.例如link.src和【‘’】此处的【‘’】不指数组,中括号别忘记加引号)用.操作符时不可以用-,要用驼峰命名或直接转换为【】使用;

对象没有length

for in语句 遍历对象语句 ,枚举语句

for(var i in obj){

}

for in 不要对数组循环,因为for in的顺序不是固定的,无序查询!

#所有的函数都有prototype属性 该属性是一个对象

##所有的对象都有一个 proto 属性

##假如这个函数没有任何属性的话 prototype对象 默认有个constructor属性

例如:fn(){}

var fn2 = new fn() //从fn方法里面new出的一个实例,生成对象

prototype 原型 当前函数的原型

__proto__指向创建当前对象的函数prototype (前面是双下划线)

__proto__本意也是原型,指对象的原型

//原型链//

//继承//

#this肯定指向一个对象本身

//在全局作用域下面调用this this指向global 也就是window

//函数声明 匿名函数 函数表达式 在全局中调用 this指向window

//new关键词作用的对象 this还是指向创建该对象

//当this当前的函数为对象的属性的时候,this指向该对象

//typeof 检测基本的数据类型

//instanceof 检测引用类型

//基本类型数据加属性不会报错 但是引用的话是undefined

//var声明的变量和function声明的函数会提到当前作用域的最顶端,而且function的声明是优先于var变量声明的(如果同时存在的话),所以提前声明后输出的值是function 然后代码往下执行a进行重新赋值了,故第二次输出是2.

//

this

function JSClass(){
    this.m_Text = 'division element';
    this.m_Element.innerHTML = this.m_text;
    this.m_Element = document.createElement('div');
    this.m_Element.addEventListener('click',this.func);
    //this.m_Element.onclick = this.func;
}
JSClass.prototype.render = function(){
    document.body.appendChild(this.m_Element);
}
JSClass.prototype.func = function(){
    alert(this.m_Text);
}
var jc = new JSClass();
jc.Render();
jc.func();

str.indexOf(‘索引字符’,’起始位置’)判断字符串中有没有字符,每次只返回第一个字符的下标值,没有找到字符的话返回-1;

var str = 'sundyfuther.github.io';
console.log(str.indexOf('u'));//输出1,说明n的第一个字符的下标是2
console.log(str.indexOf('u',3));//输出6,说明从第三个字符开始往后查n的第一个字符的下标是6

str.lastIndexOf(‘’) 从后面开始查找,但是字符的下标仍然是按照正序进行,每次返回找到的第一个字符的下标值,没有的话同样返回-1;

var str = 'sundyfuther.github.io';
console.log(str.lastIndexOf('u'));//输出16,说明从最后一个字符往前查,检索到的第一个u的字符    位置是16

str.slice(position start,position end) 可以只有起始位置,打出来是从起始位置到字符串最后,如果是两个位置都有,则是从startposition开始数(endposition-startposition)个单位,其实是不包含endpositon所在位置上的字符;

var str = 'sundyfuther.github.io';
console.log(str.slice(3,5));//输出:dy,从第3个字符开始往后检索两个,不包含第5个字符,也就是endposition位置上的字符

substring()提取相应的字符串//字符串长度是endposition-startpositon 不包含endposition

同上slice()方法

split()方法用于把字符串分割成字符串数组 用原字符串中的字符做分割,如果不包含这个字符则返回原字符串的一个数组;

var str = 'sundyfuther.github.io';
console.log(str.split('.'));//输出:["sundyfuther", "github", "io"]这样的字符串数组

Unicode编码

charCodeAt(下标值) 返回下标位置的字符的unicode编码

var str = 'sundyfuther.github.io';
console.log(str.charCodeAt(4));//输出:121具体对应的unicode值可以在相对应的unicode列表中查询

charAt(下标值) 返回指定位置的字符

var str = 'sundyfuther.github.io';
console.log(str.charAt(4));//输出'y'

toLowerCase()把字符串转换为小写

var str = 'SUNDYFUTHER.GITHUB.IO';
console.log(str.toLowerCase());//输出'sundyfuther.github.io'

toUpperCase()把字符串转换为大写

var str = 'sundyfuther.github.io';
console.log(str.toUpperCase());//输出:'SUNDYFUTHER.GITHUB.IO'5