JavaScript学习笔记之二

一 js与json数据格式的转换:序列号与反序列化

    JSON.stringify(jsobj, '  ');//将js的obj转换为json对象;

    JSON.parse()把json对象变成一个JavaScript对象,参数可以带函数以便进行灵活处理;

二正则 表达式:可进行复杂的字符串处理,匹配。

三 js的面向对象:面向对象一般有类class、实例obj,js没有class,一般通过一个已有实例来创建新的对象,方法有

  3.1 Object.create(已存在对象)来创建新的对象

        3.2新版本的用__proto__  newobj.__proto__ =oldobj;

        3.3用new构建对象:构造函数首字母应当大写,而普通函数首字母应当小写,这样,一些语法检查工具如jslint将可以帮你检测到漏写的new      

        

function Student(props) {
    this.name = props.name || '匿名'; // 默认值为'匿名'
    this.grade = props.grade || 1; // 默认值为1
}

Student.prototype.hello = function () {
    alert('Hello, ' + this.name + '!');
};
/*几个巨大的优点:一是不需要new来调用,二是参数非常灵活,可以不传*/
function createStudent(props) {
    return new Student(props || {})
}
View Code

      3.4原型集成继承:

function Student(props) {
    this.name = props.name || 'Unnamed';
}

Student.prototype.hello = function () {
    alert('Hello, ' + this.name + '!');
}

function PrimaryStudent(props) {
    Student.call(this, props);
    this.grade = props.grade || 1;
}

function inherits(Child, Parent) {
    var F = function () {};
    F.prototype = Parent.prototype;
    Child.prototype = new F();
    Child.prototype.constructor = Child;
}
// 实现原型继承链:
inherits(PrimaryStudent, Student);

// 绑定其他方法到PrimaryStudent原型:
PrimaryStudent.prototype.getGrade = function () {
    return this.grade;
};
View Code

      3.5 新的es6标准的class继承:注意基类和扩展类都用class,继承关系用extends,初始化父类用super()

class Student {
    constructor(name) {
        this.name = name;
    }

    hello() {
        alert('Hello, ' + this.name + '!');
    }
}

class PrimaryStudent extends Student {
    constructor(name, grade) {
        super(name); // 记得用super调用父类的构造方法!
        this.grade = grade;
    }

    myGrade() {
        alert('I am at grade ' + this.grade);
    }
}
View Code

 浏览器:

  windows不但充当全局作用域,而且表示浏览器窗口,属性window.innerWidth 、 window.innerHeight

       navigator对象表示浏览器的信息:浏览器名称。版本,语言等

        location对象表示当前页面的URL信息:网址、端口、协议、文件路径等

       document对象提供的getElementById()getElementsByTagName(),document.getElementsByClassName()/querySelector()querySelectorAll()可以按ID获得一个DOM节点和按Tag名称获得一组DOM节点

       history对象的back()forward (),相当于用户点击了浏览器的“后退”或“前进”按钮,尽量不用

原文地址:https://www.cnblogs.com/jieruishu/p/11971405.html