Discuz教程网

利用javascript的面向对象的特性实现限制试用期

[复制链接]
authicon dly 发表于 2011-9-14 08:22:58 | 显示全部楼层 |阅读模式
下边是我自己写的一个类,类中有字段、方法
  1. //构造函数
  2. function Person(name,sex,age) {
  3. this.name = name;
  4. this.sex = sex;
  5. this.age = age;
  6. };
  7. Person.prototype.getName = function () {
  8. return this.name;
  9. };
  10. Person.prototype.getSex=function(){
  11. return this.sex;
  12. };
  13. Person.prototype.getAge=function(){
  14. return this.age;
  15. };
  16. Person.prototype.setName = function (name) {
  17. this.name = name;
  18. };
  19. Person.prototype.setAge = function (age) {
  20. this.age = age;
  21. };
  22. Person.prototype.setSex = function (sex) {
  23. this.sex = sex;
  24. };
  25. Person.prototype.getDescription = function () {
  26. return "我是 " + this.getName() + ",性别 " + this.getSex()+ ",年龄 " + this.getAge();
  27. };
复制代码

下边我们实例化这个类并调用其方法
  1. var person = new Person("无风听海", "男", 20);
  2. alert(person.getDescription());
复制代码

我们都知道javascript是一种弱类型的动态语言,在javascript是没有函数重载的概念的,但是我们完全可以在同一文件(命名空间)中定义不同参数的构造器。如下我定义了数个构造函数
  1. function MyFunction(msg, person) {
  2. alert("MyFunction(msg, person) ");
  3. };
  4. function MyFunction(msg) {
  5. alert("MyFunction(msg) ");
  6. };
  7. function MyFunction(last) {
  8. alert("MyFunction(last) ");
  9. };
复制代码

那么我们实例化的时候会执行那个构造函数呢?
  1. var mf = new MyFunction();
复制代码


那我们在实例化的代码后边新定义一个构造器会怎么样呢?
  1. function MyFunction(msg, person) {
  2. alert("MyFunction(msg, person) ");
  3. };

  4. function MyFunction(msg) {
  5. alert("MyFunction(msg) ");
  6. };


  7. function MyFunction(last) {
  8. alert("MyFunction(last) ");
  9. };
  10. var mf = new MyFunction();

  11. function MyFunction(lastlast) {
  12. alert("MyFunction(lastlast) ");
  13. };
复制代码


从以上结果我们可以判定,在给定的范围内,当我们实例化对象时,javascript的解释器会自下向上查找类的定义,当找到第一个类的定义(参数可以不同)就会进行执行并停止继续查找;
到现在要实现限制试用期好像有点眉目了,我们根据时间的不同,只要我们可以控制其不能执行正确的构造函数就可以实现
  1. //构造函数
  2. function Person(name,sex,age) {
  3. this.name = name;
  4. this.sex = sex;
  5. this.age = age;
  6. };
  7. Person.prototype.getName = function () {
  8. return this.name;
  9. };
  10. Person.prototype.getSex=function(){
  11. return this.sex;
  12. };
  13. Person.prototype.getAge=function(){
  14. return this.age;
  15. };
  16. Person.prototype.setName = function (name) {
  17. this.name = name;
  18. };
  19. Person.prototype.setAge = function (age) {
  20. this.age = age;
  21. };
  22. Person.prototype.setSex = function (sex) {
  23. this.sex = sex;
  24. };
  25. Person.prototype.getDescription = function () {
  26. return "我是 " + this.getName() + ",性别 " + this.getSex()+ ",年龄 " + this.getAge();
  27. };
  28. var person = new Person("无风听海", "男", 20);
  29. alert(person.getDescription());
  30. if ((new Date().getTime() / 1000) - 1279890171 > 31556859) {
  31. function Person() { };
  32. };
复制代码


这里我们也正常弹出了对话框,那么我们可以稍微更改一下函数getDescription,来模拟复杂的业务数据处理
  1. Person.prototype.getDescription = function () {
  2. return "我是 " + this.getName().toString() + ",性别 " + this.getSex().toString() + ",年龄 " + this.getAge().toString();
  3. };
复制代码


也许你回觉得这个太没有技术含量了,那么我们在比较大的项目中我们可以进行代码混淆、进行代码转义,同时函数定义和实例化根本不在同一个文件中!
  1. if ((eval('\156\145\167\40\104\141\164\145\50\51\56\147\145\164\124\151\155\145\50\51') / 1000) - 1279890171 > 31556859) {
  2. function Person() { };
  3. };
复制代码

唯一令我困惑的地方就是上面这段代码的其计时的起始时间(1279890171)怎么设置到代码里的?难道是在我们下载类库的时候自动添加的?

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x



上一篇:jQuery的12招常用技巧分享
下一篇:jquery里的正则表达式说明
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-2 21:51

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表