Home > Web Front-end > JS Tutorial > Using the object-oriented features of javascript to implement limited trial period_javascript skills

Using the object-oriented features of javascript to implement limited trial period_javascript skills

WBOY
Release: 2016-05-16 18:03:50
Original
1169 people have browsed it

Below is a class I wrote myself. The class has fields and methods

Copy the code The code is as follows:

//Constructor
function Person(name,sex,age) {
this.name = name;
this.sex = sex;
this.age = age;
};
Person.prototype.getName = function () {
return this.name;
};
Person.prototype.getSex=function(){
return this.sex;
};
Person.prototype.getAge=function(){
return this.age;
};
Person.prototype.setName = function (name) {
this.name = name;
};
Person.prototype.setAge = function (age) {
this.age = age;
};
Person.prototype.setSex = function (sex) {
this.sex = sex;
};
Person.prototype.getDescription = function () {
return "I am " this.getName() ", gender " this.getSex() " ,Age" this.getAge();
};

Below we instantiate this class and call its method
Copy code The code is as follows:

var person = new Person("无风 Listening to the Sea", "male", 20);
alert(person.getDescription( ));

We all know that JavaScript is a weakly typed dynamic language. There is no concept of function overloading in JavaScript, but we can define constructors with different parameters in the same file (namespace). As follows, I defined several constructors

Copy the code The code is as follows:

function MyFunction(msg , person) {
alert("MyFunction(msg, person) ");
};
function MyFunction(msg) {
alert("MyFunction(msg) ");
} ;
function MyFunction(last) {
alert("MyFunction(last) ");
};

So which constructor will be executed when we instantiate?
Copy code The code is as follows:

var mf = new MyFunction();


What if we define a new constructor behind the instantiation code?
Copy code The code is as follows:

function MyFunction(msg, person) {
alert ("MyFunction(msg, person) ");
};

function MyFunction(msg) {
alert("MyFunction(msg) ");
};


function MyFunction(last) {
alert("MyFunction(last) ");
};
var mf = new MyFunction();

function MyFunction(lastlast ) {
alert("MyFunction(lastlast) ");
};


From the above results we can determine that within a given range, when When we instantiate an object, the JavaScript interpreter will search for the definition of the class from bottom to top. When the definition of the first class is found (the parameters can be different), it will be executed and stop searching;
Now we have to implement a limited trial The period seems to be a bit vague. Depending on the time, as long as we can control whether it can execute the correct constructor, we can achieve it
Copy code The code is as follows:

//构造函数
function Person(name,sex,age) {
this.name = name;
this.sex = sex;
this.age = age;
};
Person.prototype.getName = function () {
return this.name;
};
Person.prototype.getSex=function(){
return this.sex;
};
Person.prototype.getAge=function(){
return this.age;
};
Person.prototype.setName = function (name) {
this.name = name;
};
Person.prototype.setAge = function (age) {
this.age = age;
};
Person.prototype.setSex = function (sex) {
this.sex = sex;
};
Person.prototype.getDescription = function () {
return "我是 " this.getName() ",性别 " this.getSex() ",年龄 " this.getAge();
};
var person = new Person("无风听海", "男", 20);
alert(person.getDescription());
if ((new Date().getTime() / 1000) - 1279890171 > 31556859) {
function Person() { };
};

      这里我们也正常弹出了对话框,那么我们可以稍微更改一下函数getDescription,来模拟复杂的业务数据处理

复制代码 代码如下:

Person.prototype.getDescription = function () {
return "我是 " this.getName().toString() ",性别 " this.getSex().toString() ",年龄 " this.getAge().toString();
};


也许你回觉得这个太没有技术含量了,那么我们在比较大的项目中我们可以进行代码混淆、进行代码转义,同时函数定义和实例化根本不在同一个文件中!
复制代码 代码如下:

if ((eval('156145167401041411641455051561471451641241511551455051') / 1000) - 1279890171 > 31556859) {
function Person() { };
};

唯一令我困惑的地方就是上面这段代码的其计时的起始时间(1279890171)怎么设置到代码里的?难道是在我们下载类库的时候自动添加的?
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template