zoukankan      html  css  js  c++  java
  • 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 (),相当于用户点击了浏览器的“后退”或“前进”按钮,尽量不用

  • 相关阅读:
    App唤起微信小程序和回调
    微信小程序 — 自定义picker选择器弹窗内容+textarea穿透bug
    微信小程序的场景值scene
    微信小程序textarea层级过高(盖住其他元素)
    微信小程序如何修改本地缓存key中的单个数据
    微信小程序---查看更多的显示与隐藏
    微信小程序文字超过行后隐藏并且显示省略号
    Flutter 页面下拉刷新和上拉加载
    json转换成dart类 JSON to Dart
    Flutter 保持页面状态
  • 原文地址:https://www.cnblogs.com/jieruishu/p/11971405.html
Copyright © 2011-2022 走看看