事件调用方法+事件类型+创建对象的方法

事件调用方法一:内联调用事件

<button id="btn" onclick="sayHi()">按钮</button>

事件调用方法二:事件处理器

btn.onclick = function(){

alert('hello')

}

事件调用方法三:

btn.addEventListener('mouseover',function(){

console.log("kw33,国庆开心吗")

})

  btn.addEventListener("mouseout", function(){

            console.log('假期归来,好好学习吧~');

        });

document.onscroll = function(){

console.log('1');

}



内置对象-数组有哪些方法

Unshift(),

Shift()

Push()

Pop()

Slice()

Splice()

Reverse()

Sort()

Includes()

Join()

Concat()

forEach()

some()

every()

map()

filter()

reduce()

find()

findIndex()


事件类型有哪些

mouseover

mouseout

mouseenter

mouseleave

click

dblclick

mousedown

mouseup

mousemove

mousewheel

contextmenu


表单事件

Focus 获取焦点

失去焦点blur

Input

Change

Select

Submit

Reset

页面事件:

Scroll

Load

Resize

键盘事件:

Keydown:所有按键按下时触发

Keyup

Keypress:当按下可打印字符键时触发


阻止冒泡的方法

stopPropagation()

阻止事件默认行为的方法

preventDefault()


JS数据类型:

5种基本数据类型(原始数据类型,简单数据类型)

undefined,null,boolean,string,number

除了这五种数据类型,其他都是对象数据类型(引用数据类型,复杂数据类型)

Object

基本数据类型和对象数据类型在存储数据方式上的区别是什么?

基本数据类型:按值存储,存储在栈内存中

对象数据类型:按地址存储,存储在堆内存中

        let a = 1;

        let b = 1;

        console.log( a == b );      // true

        let x = [1]

        let y = [1]

        console.log( x == y );       // false


 创建对象的方法:

      方法一:new方法

        let person = new Object();

        // 通过点方式给对象添加属性或方法

        person.name = "张三",

        person.age = 18;

        person.play = function(){

            alert('我会打篮球!')

        }

        console.log(person);

        console.log(person.name);

        console.log(person.age);

        person.play()

        // 通过中括号添加属性或方法

        person['gender'] = '男'

        console.log(person['gender']);

        person['eat'] = function(){

            console.log('我喜欢吃冰激凌');

        }

        person['eat']()

  创建对象方法二:对象字面量(推荐)

        let person = {

            name: '李四',

            age: 19,

            gender: '女',

            play: function(){

                alert('我会踢足球')

            },

            learn: function(){

                console.log('我喜欢学前端');

            }

        };

        console.log( person.name );

        console.log( person['age'] );

        person.play();

        person.learn();

      创建对象方法三:工厂模式(了解)

        function Person(pname,pweapon,paddress){

            let obj = {};

            obj.name = pname;

            obj.weapon = pweapon;

            obj.address = paddress;

            obj.intro = function(){

                console.log('我叫' + this.name + ',我住在' + this.address);

            }

            return obj;

        }

        let p1 = Person('猪八戒','九齿钉耙','天河')

        console.log(p1.name);

        console.log(p1.address);

        p1.intro()

        let p2 = Person('孙悟空','金箍棒','花果山')

        console.log(p2.name);

        console.log(p2.address);

        p2.intro()

        let p3 = Person("白骨精",'骨头剑','荒山野岭')

        p3.intro()

        // 看不出类型

        // 判断对象的具体类型:instanceof

        console.log(p1 instanceof Object);

        console.log(p3 instanceof Object);

        console.log( [1] instanceof Array);

    创建对象方法四:构造函数(掌握)

        function God(pname,pweapon,paddress){

            this.name = pname;

            this.weapon = pweapon;

            this.address = paddress;

            this.intro = function(){

                console.log('我叫' + this.name + ',我住在' + this.address);

            }

        }

        let p1 = new God('猪八戒','九齿钉耙','天河')

        console.log(p1.name);

        console.log(p1.address);

        p1.intro()

        let p2 = new God('孙悟空','金箍棒','花果山')

        console.log(p2.name);

        console.log(p2.address);

        p2.intro()

        // let p3 = Person("白骨精",'骨头剑','荒山野岭')

        // p3.intro()

        // 可以看出类型

        // 判断对象的具体类型:instanceof

        console.log(p1 instanceof Object); // true

        console.log(p1 instanceof God); // true

        function Monster(pname,pweapon,paddress){

            this.name = pname;

            this.weapon = pweapon;

            this.address = paddress;

            this.intro = function(){

                console.log('我叫' + this.name + ',我住在' + this.address);

            }

        }

        let m1 = new Monster("白骨精",'骨头剑','荒山野岭')

        console.log(m1.name);

        console.log(m1.address);

        m1.intro()

        console.log(m1 instanceof Object); // true

        console.log(m1 instanceof God); // false

        console.log(m1 instanceof Monster); // true

        // 打印对象的构造函数:

        // ƒ Array() { [native code] }

        console.log( [1].constructor );

        console.log( [1] instanceof Array );

        let str = new String('abc')

        // ƒ String() { [native code] }

        console.log(str.constructor);

        console.log( str instanceof String );


        // 创建对象方法四:原型

        function Cat(cname, cage) {

            this.name = cname;

            this.age = cage;

            // this.play = function () {

            //     console.log(this.name + '喜欢玩毛线球');

            // }

        }

        Cat.prototype.play = function () {

            console.log(this.name + '喜欢玩毛线球');

        }

        let c1 = new Cat('滚滚', 4)

        let c2 = new Cat('滚滚', 4)

        console.log(c1.name == c2.name); // true

        console.log(c1.play == c2.play); // true

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。