LeetCode: Jump Game II [044]

Perface

如果让你实现这个页面和一些操作的,比如点击1、2、3等就在那个input text中显示,还有删除功能,拨打我们先不要管它,只是模拟而已。要是我刚开始做的话,我会这样做:

  1. 用css、HTML布局那个界面
  2. 用javascript的事件委托监听那个按钮的父节点的点击事件

但是如果我想用面向对象的思想做呢?我是用Ext做的,所以我想说的是它帮我封装了很多。可能一些没用过Ext的人不太了解我下面贴的代码,但是我会尽量解释清楚的!

Description

ContactTelPanel =Ext.extend(Ext.Panel, {
	//构造方法
	constructor : function(config) {
		Ext.apply(this, config);//直接把config对象的属性全复制到this对象中
		Parent = this.parent;
		var me = this;
		ContactTelPanel.superclass.constructor.call(this, {//用ContactTelPanel的父类也就是Ext.Panel的构造函数
			autoScroll : true,
			title : "拨打电话",//设置title,跟这篇文章的主体没关系,不要管他
			id : "contacttelpanel",
			bodyStyle : "padding: 30px 300px;",
			defaults : {//可以为该对象(ContactTelPanel)包含的组件(也就是在items配置选项)设置一些相同属性
				layout : "column",
				defaults : {
					xtype : "button",
					width : 50,
					height : 25,
					style : "margin:4px 15px",
					handler : this.press //为每个按钮都添加一个click的事件
				},
				bodyBorder : false
			},
			items : [ {//textfield组件
				height : 30,
				width : 250,
				xtype : "textfield",
				id : "tf",
				style : "margin-bottom:10px"
			}, {// 没有xtype就是默认为panel,下面也是,不然就不要纠结了,直接在这里想象成第一行按钮1、按钮2、按钮3
				items : [ {
					text : "1"
				}, {
					text : "2"
				}, {
					text : "3"
				} ]
			}, {// 这里是按钮4、按钮5、按钮6

				items : [ {
					text : "4"
				}, {
					text : "5"
				}, {
					text : "6"
				} ]
			}, {// 这里是按钮7、按钮8、按钮9 下同
				items : [ {
					text : "7"
				}, {
					text : "8"
				}, {
					text : "9"
				} ]
			}, {
				items : [ {
					text : "*"
				}, {
					text : "0"
				}, {
					text : "#"
				} ]
			}, {
				items : [ {
					text : "拨打",
				}, {
					text : "删除",
				} ]
			} ]
		});
	},
	press : function() {
		var text = this.text, textfield = Ext.getDom("tf");
		if (/[0-9*#]/.test(text)) {//在textfield中显示所点击按钮的数字
			textfield.value += text;
		} else if (this.text == "删除") {//删除功能
			textfield.value = textfield.value.slice(0, -1);
		} else if (this.text == "拨打") {//这个先不要管他
			Tel.telcall(textfield.value);
		}
	}

});

注:其实从上面可以知道ContactTelPanel是继承Ext.Panel,然后这个面板中有很多个键,每个键都监听click事件。确实在这里觉得自己敲得不是很好,应该是用事件委托来实现,因为你每个按钮都监听了click事件,太影响效率了。用事件委托我们可以指监听它的父节点的click事件就行了,然后根据事件流来判断目的对象并操作。本文重点还是监听事件里面handler : this.press这段代码中
。我遇见的问题就是如果我在press函数要用到这个类ContactTelPanel的一些属性,怎么办?

Idea

我在想,我要在press函数中用到这个类的属性,我直接在press用this对象来获取不就行了,但是我错了,比如你在press函数中console.dir(this),看chrome控制台出现的是什么?不幸的是它出现的是Button对象,它的this指针改了。确实有点麻烦,然后我就想了三个方法,如下:

Solution

1 在每个监听事件的函数中传参
代码:handler : this.press(this),然后在press函数体中写alert(arguments[0])
出现的情况:确实在这个页面加载的时候就弹出窗口是ContactTelPanel,但是你点击那些按钮的时候它没出现了
原因:this.press(this),这样子写javascript解析器会当作调用press函数,然后在你加载页面就执行了
2 在这个ContactTelPanel类中设置全局变量
代码:比如在第五行设置me = this,然后在press函数体中写alert(me)
出现的情况:确实可以在点击按钮的时候弹出窗口,成功了
缺点:污染全局变量,容易被别人无意篡改。比如我在引入这个页面的js后面再引用其他js的时候,在后面的js中设置var me = "monkindey",那么你再点击那个页面的按钮的时候它会弹出123,为不是ContactTelPanel对象
3 简单运用了闭包
代码:handler : function(){me.press(me)} 注:me就是ContactTelPanel对象,因为在function中this指针已经是button对象了,所以应该在function外面用me(或者其他变量名)保存this对象,即var me = this
出现的情况:这个当然是成功
4 用call来实现函数绑定
代码:handler : function(){ me.press.call(this,me);}
个人觉得:这个应该才是最好用的

LeetCode: Jump Game II [044],布布扣,bubuko.com

时间: 2024-10-17 14:54:50

LeetCode: Jump Game II [044]的相关文章

[LeetCode] Jump Game II(贪婪算法)

Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps

[leetcode]Jump Game II @ Python

原题地址:https://oj.leetcode.com/problems/jump-game-ii/ 题意: Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal i

LeetCode: Jump Game II 解题报告

Jump Game II Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum nu

[LeetCode] Jump Game II 贪心

Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps

leetcode - Jump Game II

Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps

[LeetCode] Jump Game II 跳跃游戏之二

Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps

[LeetCode]Jump Game II (贪心,维护当前最远能到达的位置和所需最少步数)

最少跳跃步数 第一想法是DP,复杂度O(n^2),但是对于大型数据会超时. Discuss中一种犀利的贪心方法,复杂度为O(n) class Solution { public: int jumpDP(int A[], int n) {//DP方法 int *dp=new int[n],j; memset(dp,127,sizeof(int)*n); dp[0]=0; int i=0; for(i=0;i<n-1;++i){ for(j=1;j<=A[i]&&i+j<n;

【To Read】LeetCode | Jump Game II(转载)

题目: Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of j

【贪婪算法、动态规划】Jump Game II

题目:leetcode Jump Game II Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in th