js中call(),apply(),以及prototype的含义
最近段时间主要学习前端去了,然而所遇到的一些问题我觉得有必要去深究一下
prototype:
1 js中有三种表达方法
类方法,属性方法,原型方法
function People(name) {
this.name=name;
//对象方法
this.Introduce=function(){
console.log("My name is "+this.name);
}
}
//类方法
People.Run=function(){
console.log("I can run");
}
//原型方法
People.prototype.IntroduceChinese=function(){
console.log("我的名字是"+this.name);
}
//测试
var p1=new People("xx");
p1.Introduce(); // My name is xx
People.Run(); //I can run
p1.IntroduceChinese(); 我的名字是xx
其实从上面可以看出prototype,实际上向people中添加了一个方法,而这也应官方的解释“prototype 属性使您有能力向对象添加属性和方法"
2 实现继承
function baseClass(){
this.showMessage = function () {
console.log('baseClass:','woc this is bad boy')
}
}
// function extendClass(){}
function extendClass(){
this.showMessage = function () {
console.log('extendClass:', 'woc this is good body')
}
}
function extendClass1(){}
extendClass.prototype = new baseClass()
extendClass1.prototype = new baseClass()
var eC = new extendClass() //extendClass: woc this is good body
var eC1 = new extendClass1() //baseClass: woc this is bad boy
eC.showMessage()
eC1.showMessage()
从上面的案例可以看出如果extendClass()的showMessage存在的情况就会指向自己,如果不存在就会指向其”父类“
call() 和 appyl()
1 每个function中有一个prototype, call(), apply()
call() apply() 我简单的理解为改变你当前对象的指向,这可能有点抽象,看下代码
function method1(arg1, arg2) {
return arg1+arg2
}
function method2(arg1, arg2) {
return arg1-arg2
}
var result1 = method2.apply(method1,[3,2]);
var result2 = method1.call(method2,3,3)
console.log(result1); //1
console.log(result2); //6
从上面的实例可以看出两个function的指向发上了改变
call() apply(): 这个是当前的this指针指向调用你的那个function(有点类似copy的意思)
而两者的区别在于apply() 在参数上只有两个参数(当前方法,数组),
而call()的参数则是单个单个的形式
2 实现继承
function father(word) {
this.word = word
this.showName1 = function(){
console.log('Father say:', this.word)
}
}
function mother(word) {
this.word = word
this.showName2 = function () {
console.log('Mother say:', this.word)
}
}
function child(word) {
// father.apply(this,[word])
father.call(this, word)
mother.call(this, word)
}
var c = new child('boys');
c.showName1(); // Father say: boys
c.showName2(); // Mother say: boys
3 好的案例
(1)活用
var result = Math.max(7.25,7.30) var array = [1,3,4,5,6,0,32.3,3.3] var result1 = Math.max.apply(null,array);
var result2 = Math.min.apply(null,array);
console.log(result) // 7.3
console.log(result1) // 32.3
console.log(result2) // 0
在js Math.max()中的参数是没有传数组的形式的,而这里通过apply()巧妙地实现了这种转变,首先我们并不需要那个对象去指向Math,所以放了一个null做为参数,然后将arary数组传入其中
(2) 理解
function baseClass() {
this.showMsg = function()
{
console.log("baseClass::showMsg");
}
this.baseShowMsg = function()
{
console.log("baseClass::baseShowMsg");
}
}
baseClass.showMsg = function()
{
console.log("baseClass::showMsg static");
}
function extendClass()
{
this.showMsg =function ()
{
console.log("extendClass::showMsg");
}
}
extendClass.showMsg = function()
{
console.log("extendClass::showMsg static")
}
extendClass.prototype = new baseClass();
var instance = new extendClass();
instance.showMsg(); //显示extendClass::showMsg
instance.baseShowMsg(); //显示baseClass::baseShowMsg
instance.showMsg(); //显示extendClass::showMsg
baseClass.showMsg.call(instance);//显示baseClass::showMsg static
var baseinstance = new baseClass();
baseinstance.showMsg.call(instance);//显示baseClass::showMsg
js中call(),apply(),以及prototype的含义的更多相关文章
- 深入理解js中的apply、call、bind
概述 js中的apply,call都是为了改变某个函数运行时的上下文环境而存在的,即改变函数内部的this指向. apply() apply 方法传入两个参数:一个是作为函数上下文的对象,另外一个是作 ...
- js中的apply和call API
借用网上的一个例子: fun.call(this,arg1,arg2,arg3) fun.apply(this,arguments) this.fun(arg1,arg2,arg3) 三种方法等效. ...
- Js中的apply和call
1.call和apply都是为了改变某个函数运行时的上下文而存在的 2.也就是改变函数体内this的指向. 3.二者的作用完全一样,只是接受参数的方式不太一样. 4.call 需要把参数按顺序传递进去 ...
- 分析js中的constructor 和prototype
在javascript的使用过程中,constructor 和prototype这两个概念是相当重要的,深入的理解这两个概念对理解js的一些核心概念非常的重要. 我们在定义函数的时候,函数定义的时候函 ...
- JavaScript中call,apply和prototype
[TOC] call()方法 语法:call([thisObj[,arg1[, arg2[, [,.argN]]]]]) 定义:调用一个对象的一个方法,以另一个对象替换当前对象. 如果没有提供 thi ...
- 深入分析js中的constructor 和prototype
在javascript的使用过程中,constructor 和prototype这两个概念是相当重要的,深入的理解这两个概念对理解js的一些核心概念非常的重要. 我们在定义函数的时候,函数定义的时候函 ...
- 关于JS中的constructor与prototype
======================================================================== 在学习JS的面向对象过程中,一直对constructo ...
- 【JavaScript】关于JS中的constructor与prototype
最初对js中 object.constructor 的认识: 在学习JS的面向对象过程中,一直对constructor与prototype感到很迷惑,看了一些博客与书籍,觉得自己弄明白了,现在记录如下 ...
- 【推荐】关于JS中的constructor与prototype【转】
最初对js中 object.constructor 的认识: 在学习JS的面向对象过程中,一直对constructor与prototype感到很迷惑,看了一些博客与书籍,觉得自己弄明白了,现在记录如下 ...
随机推荐
- 嵌入式C语言自我修养 04:Linux 内核第一宏:container_of
4.1 typeof 关键字 ANSI C 定义了 sizeof 关键字,用来获取一个变量或数据类型在内存中所占的存储字节数.GNU C 扩展了一个关键字 typeof,用来获取一个变量或表达式的类型 ...
- STM32(6)——USART串口的使用
1. 串口的基本概念 在STM32的参考手册中,串口被描述成通用同步异步收发器(USART),它提供了一种灵活的方法与使用工业标准NRZ异步串行数据格式的外部设备之间进行全双工数据交换.USART利用 ...
- Selenium_python自动化跨浏览器执行测试(简单多线程案例)
发生背景: 跨浏览器测试是功能测试的一个分支,用以验证web应用在不同浏览器上的正常工作,通常情况下,我们都期望web类应用能够被我们的用户在任何浏览器上使用,例如有的人喜欢IE浏览器上使用,有的人喜 ...
- 树莓派 raspberry系统 VNC View 连接 Cannot currently show the desktop 错误解决
https://www.raspberrypi.org/forums/viewtopic.php?t=216737 我是因为空间不够
- MFC 程序退出方法
基於對話框的: 1.PostQuitMessage(0);2.PostMessage(WM_QUIT,0,0);3.ExitProcess(0);注意使用时先释放分配的内存,以免造成内存泄露4.exi ...
- 第六节 Go数据结构之集合
一.什么是集合 集合就是不同对象的无序聚集.那么链表和集合有什么关系呢?我们来变个魔术.如下图是各种颜色组成的链表: 下面我们一步步把链表变成集合. 第一步砍去链接 第二步去掉重复 第三步放到一个框里 ...
- DP_括号匹配序列问题
括号匹配问题 简单括号匹配问题是给出字符串,判断字符串中的括号是否匹配,此类问题核心解决方案就是利用栈的后进先出的特性,从左到右依次遍历字符串,遇左括号进栈,遇右括号将其与栈顶元素配对,若能配对,则栈 ...
- 20155211 2016-2017-2 《Java程序设计》第四周学习总结
20155211 2016-2017-2 <Java程序设计>第四周学习总结 教材学习内容总结 这周的内容感觉较上周相比难度增加 教材学习中的问题和解决过程 刚开始学习第六章的时候的时候敲 ...
- 20155213 2016-2017-2《Java程序设计》第四周学习总结
20155213 2016-2017-2<Java程序设计>第四周学习总结 教材学习内容总结 继承与多态 继承 继承避免多个类间重复定义共同行为,使用关键字extends.继承表明了子类与 ...
- 20155329实验二 Java面向对象程序设计
实验二 Java面向对象程序设计 1实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 2. 初步掌握单元测试 ...